Description
Bessie hears that an extraordinary meteor shower isComing Reports say that these meteors would crash into Earth and destroy anything they hit. Anxious forHer safety, she vows-find her-to-a safe location isNever destroyed by a meteor). She isCurrently grazing at the origininchThe coordinate plane and wants to move to aNew, safer location whileavoiding being destroyed by meteors along her. The reports say that M Meteors (1≤m≤ -, the) would strike, with meteor I'll striking point (Xi, Yi) (0≤xi≤ -;0≤yi≤ -) at Time Ti (0≤ti≤1, the). Each of the meteor destroys the point, it strikes and also the four rectilinearly adjacent lattice points. Bessie leaves the Origin at time0and can travelinchThe first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often4) 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 isdestroyed). Determine the minimum time it takes Bessie toGetto a safe place.
Input
1 : A single integer:m 2.. m+1: Line i+1 contains three space-separated integers:xi, Yi, and Ti
Output
1 get to a safe place or-1if is impossible.
Sample Input
4 0 0 2 2 1 2 1 1 2 0 3 5
Sample Output
5
Source
Usaco February Silver
There is a small wenqing to see the meteor shower, but the meteor fell down will be smashed up and down about five points. Each meteor falls the position and the time are different, asks the small wenqing can live, if can live, the shortest escape time is how much?
There is a place to write wrong, WA for a long time, too careless//code 72 lines
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <stack>5#include <vector>6#include <map>7#include <queue>8 using namespacestd;9 #defineN 506Ten intMp[n][n]; One intdirx[]={0,0,-1,1}; A intdiry[]={-1,1,0,0}; - intVis[n][n]; - structnode{ the intx,y,t; - }; - intBFs () { - if(mp[0][0]==-1)return 0; + if(mp[0][0]==0)return-1; - Node S; +s.x=0; As.y=0; ats.t=0; -vis[0][0]=1; -Queue<node>Q; - Q.push (s); - Node t1,t2; - while(!Q.empty ()) { int1=Q.front (); - Q.pop (); to for(intI=0;i<4; i++){ +t2.x=t1.x+Dirx[i]; -t2.y=t1.y+Diry[i]; thet2.t=t1.t+1; * if(t2.x<0|| T2.x>=n | | t2.y<0|| T2.y>=n)Continue; $ if(mp[t2.x][t2.y]==-1){Panax Notoginseng returnt2.t; - } the if(T2.t>=mp[t2.x][t2.y])Continue; + if(Vis[t2.x][t2.y])Continue; Avis[t2.x][t2.y]=1; the //mp[t2.x][t2.y]=t2.t; + Q.push (T2); - } $ } $ return-1; - } - intMain () the { - intm;Wuyi while(SCANF ("%d", &m) = =1){ thememset (mp,-1,sizeof(MP)); -memset (Vis,0,sizeof(Vis)); Wu for(intI=0; i<m;i++){ - intx,y,t; Aboutscanf"%d%d%d",&x,&y,&t); $ if(mp[x][y]==-1){//processing (x, y) this -mp[x][y]=T; - } - Else{ Amp[x][y]=min (mp[x][y],t); + } the - for(intj=0;j<4; j + +) {//handle around 4 points $ intxx=x+Dirx[j]; the intyy=y+Diry[j]; the if(xx<0|| Xx>=n | | yy<0|| Yy>=n)Continue; the if(mp[xx][yy]==-1){ themp[xx][yy]=T; - } in Else{ themp[xx][yy]=min (mp[xx][yy],t); the } About } the } the theprintf"%d\n", BFS ()); + - } the return 0;Bayi}
View Code
POJ 3669 Meteor shower (BFS)