1. 程式人生 > >洛谷 P2895 [USACO08FEB]流星雨Meteor Shower

洛谷 P2895 [USACO08FEB]流星雨Meteor Shower

content memset port scanf coo nbsp 左右 font iou

P2895 [USACO08FEB]流星雨Meteor Shower

題目描述

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

牛去看流星雨,不料流星掉下來會砸毀上下左右中五個點。每個流星掉下的位置和時間都不同,求牛能否活命,如果能活命,最短的逃跑時間是多少?

輸入輸出格式

輸入格式:

  • Line 1: A single integer: M

  • Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

輸出格式:

  • Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

輸入輸出樣例

輸入樣例#1:
4
0 0 2
2 1 2
1 1 2
0 3 5
輸出樣例#1:
5
思路:爆搜。
錯因:本題應該用寬搜,用了深搜。
吐槽:大佬竟然是這樣判斷寬搜深搜的:深搜TLE就是寬搜,寬搜MLE/RE就是深搜╮(╯▽╰)╭。
63分的深搜:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int ans=0x7f7f7f7f,m,t,map[310][310],mm[310][310];
void dfs(int x,int y,int step){
    if(map[x][y]>2000000000){
        ans=min(ans,step);
        return ;
    }
    for(int i=0;i<4;i++){
        int cx=x+dx[i];
        int cy=y+dy[i];
        if(map[cx][cy]>step&&cx>=0&&cy>=0){
            map[cx][cy]-=1;
            dfs(cx,cy,step+1);
            map[cx][cy]+=1;
        }
    }
}
int main(){
    scanf("%d",&m);
    memset(map,0x7f,sizeof(map));
    for(int i=1;i<=m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        map[x][y]=min(map[x][y],z);
        map[x+1][y]=min(map[x+1][y],z);
        map[x][y+1]=min(map[x][y+1],z);
        if(x>0)    map[x-1][y]=min(map[x-1][y],z);
        if(y>0)    map[x][y-1]=min(map[x][y-1],z);
    }
    dfs(0,0,1);
    if(ans!=2139062143)    cout<<ans-1;
    else cout<<"-1";
}

AC的寬搜:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define oo 1e9
using namespace std;
struct nond{
    int x,y,z;
};
queue<nond>que;
bool b[310][310];
int n,map[310][310];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int main(){
    memset(map,0x7f,sizeof(map));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        map[x][y]=min(map[x][y],z);
        map[x+1][y]=min(map[x+1][y],z);
        map[x][y+1]=min(map[x][y+1],z);
        if(x>0)    map[x-1][y]=min(map[x-1][y],z);
        if(y>0)    map[x][y-1]=min(map[x][y-1],z);
    }
    nond now,pre;
    b[0][0]=1;
    now.x=now.y=now.z=0;
    que.push(now);
    while(!que.empty()){
        now=que.front();
        que.pop();
        if(map[now.x][now.y]==2139062143){
            printf("%d",now.z);
            return 0;
        }
        for(int i=0;i<4;i++){
            int cx=now.x+dx[i];
            int cy=now.y+dy[i];
            if(cx>=0&&cy>=0&&!b[cx][cy]&&map[cx][cy]>now.z+1){
                b[cx][cy]=1;
                pre.x=cx;pre.y=cy;pre.z=now.z+1;
                que.push(pre);
            }
        }
    }
    cout<<"-1";
    return 0;
}



洛谷 P2895 [USACO08FEB]流星雨Meteor Shower