POJ 3669 Meteor Shower 普通BFS

来源:互联网 发布:天津网络微信投票公司 编辑:程序博客网 时间:2024/06/07 05:56

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21251 Accepted: 5544

Description

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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.

Input

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: XiYi, and Ti

Output

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

Sample Input

40 0 22 1 21 1 20 3 5

Sample Output

5

Source

USACO 2008 February Silver

[Submit]   [Go Back]   [Status]   [Discuss]

传送门:http://poj.org/problem?id=3669


思路:直接BFS找路,开一个数组time[ ][ ]保存坐标爆炸时间,初始化 -1(存在0时刻爆炸);


AC代码:

#include<cstdio>#include<queue>#include<cstring>using namespace std;const int maxn=300+50;struct point{    int x,y;};int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};int step[maxn][maxn];int time[maxn][maxn];int vis[maxn][maxn];int bfs(point& st){        queue<point> q;        q.push(st);        while(!q.empty()){            st=q.front(); q.pop();            if(time[st.x][st.y]==-1) return 1;            for(int i=0;i<4;i++){                point a;                a.x=st.x+dx[i];                a.y=st.y+dy[i];                if(a.x>=0&&a.y>=0){                    step[a.x][a.y]=step[st.x][st.y]+1;                    if(!vis[a.x][a.y]&&(step[a.x][a.y]<time[a.x][a.y]||time[a.x][a.y]==-1)){                        vis[a.x][a.y]=1;                        //printf("(%d, %d)---",a.x,a.y);                       // printf("%d----%d\n",step[a.x][a.y],time[a.x][a.y]);                        q.push(a);                    }                }            }        }        return 0;}int main(){    int m;    while(scanf("%d",&m)!=EOF){        if(m<1) break;        memset(time,-1,sizeof(time));        memset(step,0,sizeof(step));        memset(vis,0,sizeof(vis));        for(int i=0;i<m;i++){            int x,y,t;            scanf("%d %d %d",&x,&y,&t);            if(time[x][y]!=-1) time[x][y]=min(time[x][y],t);            else time[x][y]=t;            for(int i=0;i<4;i++){                int x1=x+dx[i];                int y1=y+dy[i];                if(x1>=0&&y1>=0) {                        if(time[x1][y1]!=-1) time[x1][y1]=min(time[x1][y1],t);                        else time[x1][y1]=t;                }            }        }        point st;        st.x=0; st.y=0;        step[0][0]=0;        if(bfs(st)) printf("%d\n",step[st.x][st.y]);        else printf("-1\n");    }    return 0;}


原创粉丝点击