码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3669 Meteor Shower

时间:2014-09-25 23:43:07      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

http://poj.org/problem?id=3669

题意:

给定几个坐标,在这些坐标上 t 时刻会有陨石雨,上下左右也被损坏。怎样在最短的时间内找到一个安全的地方。

思路:预处理每个陨石下落的周围的点,然后bfs就可以。

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <algorithm>
 5 #define maxn 50000
 6 using namespace std;
 7 
 8 int n;
 9 int ans,max1;
10 int vis[400][400];
11 int dir[5][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};
12 struct node
13 {
14     int x,y;
15     int t;
16 }p[maxn],st1,st2;
17 
18 void bfs(int x,int y)
19 {
20     queue<node>q;
21     st1.x=x;
22     st1.y=y;
23     st1.t=0;
24     if(vis[x][y]==0)
25     {
26         return;
27     }
28     q.push(st1);
29     while(!q.empty())
30     {
31         st2=q.front(); q.pop();
32         if(vis[st2.x][st2.y]==-1)
33         {
34             ans=st2.t;
35             return ;
36         }
37         for(int i=0; i<5; i++)
38         {
39             int x1=st2.x+dir[i][0];
40             int y1=st2.y+dir[i][1];
41             if(!(x1>=0&&y1>=0)) continue;
42             if((vis[x1][y1]>st2.t+1)||(vis[x1][y1]==-1))
43             {
44                 if(vis[x1][y1]!=-1)
45                 {
46                     vis[x1][y1]=st2.t+1;
47                 }
48                 st1.x=x1;st1.y=y1;
49                 st1.t=st2.t+1;
50                 q.push(st1);
51             }
52         }
53     }
54 }
55 
56 int main()
57 {
58     while(scanf("%d",&n)!=EOF)
59     {
60         ans=-1;
61         memset(vis,-1,sizeof(vis));
62         for(int i=1; i<=n; i++)
63         {
64             scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].t);
65             for(int j=0; j<5; j++)
66             {
67                 int xx=p[i].x+dir[j][0];
68                 int yy=p[i].y+dir[j][1];
69                 if(!(xx>=0&&yy>=0)) continue;
70                 if(vis[xx][yy]==-1)
71                 {
72                     vis[xx][yy]=p[i].t;
73                 }
74                 else
75                 {
76                     vis[xx][yy]=min(vis[xx][yy],p[i].t);
77                 }
78             }
79         }
80         bfs(0,0);
81         printf("%d\n",ans);
82     }
83     return 0;
84 }
View Code

 

poj 3669 Meteor Shower

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/fanminghui/p/3993782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!