Meteor Shower
Description
Bessie hears that a extraordinary meteor shower is coming; Reports say that these meteors would crash into Earth and destroy anything they hit. Anxious for her safety, she vows to find she's a safe location (one that's never destroyed by a meteor). She is currently grazing at the origin of the coordinate plane and wants to move to a new, safer location while avoiding B Eing destroyed by meteors along her.
The reports say that M Meteors (1≤ M ≤50,000) would strike, with Meteor I 'll striking point (Xi, Yi) (0≤ Xi ≤ 300; 0≤ Yi ≤300) at time ti (0≤ ti ≤1,000). Each of the meteor destroys the point, 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 Distan Ce unit per second to any of the (often 4) adjacent rectilinear points that is 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: Xi, Yi, 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
Test instructions: A 2b is going to play a big adventure in the next meteor shower.
The array maze records the time at which the point cannot go.
Note: Initialization is initialized to-1, or INF
The pit-point was written in a note.
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 6 using namespacestd;7 8 Const intmaxn=310;9 Ten intMAZE[MAXN][MAXN]; One BOOLVIS[MAXN][MAXN]; A - structNode - { the intX,y,time; - }; - - intdx[4]={0,0,-1,1}; + intdy[4]={1,-1,0,0}; - + voidInitmaze (intm) A { atmemset (maze,-1,sizeof(Maze)); - - intu,v,w; - - for(intI=1; i<=m;i++) - { inscanf" %d%d%d",&u,&v,&W); - if(maze[u][v]==-1|| Maze[u][v]>W) to { +maze[u][v]=W; - } the for(intj=0;j<4; j + +) * { $ intdu=u+Dx[j];Panax Notoginseng intdv=v+Dy[j]; - if(du<0|| Du>305|| dv<0|| Dv>305) the Continue; + if(maze[du][dv]==-1|| Maze[du][dv]>W) Amaze[du][dv]=W; the } + - } $ } $ - intBFS () - { thememset (Vis,false,sizeof(Vis)); - Wuyi Node start; theStart.x=start.y=start.time=0; - Wu - //because this place WA several times, if Maze[0][0]=-1 can walk, About //and this is the direct output-1 . $ /* - if (maze[0][0]<=1) - return-1; - */ A +Queue<node>que; the while(!que.empty ()) - Que.pop (); $ the the Que.push (start); the thevis[0][0]=true; - in while(!que.empty ()) the { theNode cur=Que.front (); About Que.pop (); the the if(maze[cur.x][cur.y]==-1) the returnCur.time; + - for(intI=0;i<4; i++) the {Bayi Node CNT; thecnt.x=cur.x+Dx[i]; thecnt.y=cur.y+Dy[i]; -cnt.time=cur.time+1; - the //at first, this 305 was written in 300. the //in fact, he could have escaped to 301,302 . the //So if we don't put these points in the queue, the //Maybe you can't find a safe place, and it's wrong. - the if(cnt.x<0|| Cnt.x>305|| cnt.y<0|| Cnt.y>305) the Continue; the 94 if(Vis[cnt.x][cnt.y]) the Continue; the if(maze[cnt.x][cnt.y]!=-1&&maze[cnt.x][cnt.y]<=cnt.time) the Continue;98 Que.push (CNT); Aboutvis[cnt.x][cnt.y]=true; - }101 }102 return-1;103 }104 the intMain ()106 {107 intm;108 while(SCANF ("%d", &m)! =EOF)109 { the Initmaze (m);111 the intans=BFS ();113 theprintf"%d\n", ans); the the //Debug with117 /*118 for (int i=4;i>=0;i--)119 { - for (int j=0;j<5;j++)121 printf ("%d", maze[i][j]);122 printf ("\ n");123 }124 */ the }126 127 return 0; -}
View Code
POJ 3669 Meteor shower simple bfs with pit point