poj-3669 Meteor Shower

Meteor Shower
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16508   Accepted: 4331

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

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5

Source

 
思路:1.设置损毁地图destroy[maxn][maxn],记录每个point的损毁最早的时间。
2.使用BFS,用queue实现。
3.一旦遇到destroy[i][j]==INF(安全点),就停止搜索,当前安全点就是最近的安全点(因为使用的是BFS)。
4.遇到损毁点,只要到达的时间比损毁的最早时间还要早就是合法的走法。
 
code:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=306;
 9 #define MIN(x,y) (x<y? x:y)
10 #define MAX(x,y) (x>y? x:y)
11 #define INF 0x3f3f3f3f
12 static int destroy[maxn][maxn];
13 static int visit[maxn][maxn];
14 static int direction[5][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}};
15 static int step;
16 
17 typedef struct Point{
18     int x,y,time;
19     Point(int i=0,int j=0,int k=0):x(i),y(j),time(k) {}
20 }P;
21 
22 
23 
24 void bfs()
25 {
26     memset(visit,0,sizeof(visit));
27     queue<P> temp;
28     temp.push(P(0,0));
29     visit[0][0]=true;
30     while(temp.size())
31     {
32         P p=temp.front();
33         temp.pop();
34         if(destroy[p.x][p.y]==INF)
35         {
36             step=p.time;
37             return;
38         }
39 
40         for(int i=0;i<4;i++)
41         {
42             int xi=p.x+direction[i][0];
43             int yi=p.y+direction[i][1];
44             if(xi>=0&&yi>=0&&!visit[xi][yi]&&destroy[xi][yi]>p.time+1)
45             {
46                 temp.push(P(xi,yi,p.time+1));
47                 visit[xi][yi]=1;
48             }
49         }
50     }
51 }
52 
53 int main()
54 {
55     int num;
56     int x,y,t;
57     while(scanf("%d",&num)!=EOF)
58     {
59         step=0;
60         memset(destroy,0x3f,sizeof(destroy));
61         for(int i=0;i<num;i++)
62         {
63             cin>>x>>y>>t;
64             destroy[x][y]=MIN(destroy[x][y],t);
65             for(int j=0;j<4;j++)
66             {
67                 int xi=x+direction[j][0];
68                 int yi=y+direction[j][1];
69                 if(xi>=0&&yi>=0)
70                 {
71                     destroy[xi][yi]=MIN(destroy[xi][yi],t);
72                 }
73             }
74         }
75         if(destroy[0][0]==0)
76         {
77             printf("-1\n");
78             return 0;
79         }
80 
81         bfs();
82 
83         printf(step>0? "%d\n":"-1\n",step);
84     }
85     return 0;
86 }

 

posted @ 2016-10-19 22:31  Pacific-hong  阅读(271)  评论(0编辑  收藏  举报