POJ 3669 - Meteor Shower

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 using namespace std;
 5 int dx[4]={+1,0,-1,0};
 6 int dy[4]={0,+1,0,-1};
 7 struct type{
 8     int x,y,t;
 9 }meteor[50000+5],now,next;
10 int n,map[305][305],latest;
11 bool vis[305][305];
12 int bfs()
13 {
14     memset(vis,0,sizeof(vis)); //初始化标记数组 
15     queue<type> q;
16     now.x=0;
17     now.y=0;
18     now.t=0;
19     vis[now.x][now.y]=1;
20     q.push(now);
21     while(!q.empty())
22     {
23         now=q.front();q.pop();
24         if(map[now.x][now.y] == 2139062143) return now.t; //如果当前这个格子的被流星摧毁时间是正无穷,那么已经安全
25         for(int i=0;i<4;i++)
26         {
27             next.x=now.x+dx[i] , next.y=now.y+dy[i] , next.t=now.t+1 ;
28             if(next.x >= 0 && next.y >= 0 && map[next.x][next.y] > next.t && !vis[next.x][next.y])
29             {
30                 vis[next.x][next.y]=1;
31                 q.push(next);
32             }
33         }
34     }
35     return -1;
36 }
37 int main()
38 {
39     while(scanf("%d",&n) != EOF)
40     {
41         memset(map,0x7F,sizeof(map)); //先将地图上每个格子初始化为“正无穷” 
42         latest=-1;
43         for(int i=1;i<=n;i++)
44         {
45             scanf("%d %d %d",&meteor[i].x,&meteor[i].y,&meteor[i].t);
46             
47             if(map[meteor[i].x][meteor[i].y] > meteor[i].t) map[meteor[i].x][meteor[i].y] = meteor[i].t; 
48             for(int j=0;j<4;j++){
49                 next.x=meteor[i].x+dx[j];
50                 next.y=meteor[i].y+dy[j];
51                 if(map[ (next.x) ][ (next.y) ] > meteor[i].t) map[ (next.x) ][ (next.y) ] = meteor[i].t;
52             } //将地图上的每个格子更新,使其值成为该格最早被流星摧毁的时间 
53              
54         }
55         printf("%d\n",bfs());
56     }
57 }

 


posted @ 2017-03-21 22:26  Dilthey  阅读(290)  评论(0编辑  收藏  举报