Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21055 Accepted: 5499 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 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 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: Xi, Yi, and TiOutput
* 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 5Sample Output
5
题意:
输入m组数,每组分别代表x,y坐标和陨石落在改点时的时间t。从零点出发,问是否有生还的可能。
设mp数组初始值为-1,然后存入时间t。
用bfs层次遍历直到找出mp==-1的点,返回现在的t。
AC代码:
1 //#include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include
14 #include 15 #include 16 using namespace std;17 18 const int MAX=310;19 20 struct node{21 int x;int y;int t;22 }s,temp;23 24 int m;25 int mp[MAX][MAX];26 int dir[5][2]={ { 0,0},{ 1,0},{ 0,1},{-1,0},{ 0,-1}};27 queue q;28 29 int bfs(){30 if(mp[0][0]==-1) return 0;31 if(mp[0][0]==0) return -1;32 s.x=0;33 s.y=0;34 s.t=0;35 q.push(s);36 while(!q.empty()){37 temp=q.front();38 q.pop();39 for(int i=0;i<5;i++){40 s.x=temp.x+dir[i][0];41 s.y=temp.y+dir[i][1];42 s.t=temp.t+1;43 if(s.x<0||s.x>=MAX||s.y<0||s.y>=MAX) continue;44 if(mp[s.x][s.y]==-1) return s.t;45 if(mp[s.x][s.y]<=s.t) continue;46 mp[s.x][s.y]=s.t;47 q.push(s);48 }49 }50 return -1;51 }52 53 int main(){54 ios::sync_with_stdio(false);55 cin>>m;56 memset(mp,-1,sizeof(mp));57 while(m--){58 int x,y,t;59 cin>>x>>y>>t;60 for(int i=0;i<5;i++){61 int nx=x+dir[i][0];62 int ny=y+dir[i][1];63 if(nx<0||nx>=MAX||ny<0||ny>=MAX)64 continue;65 if(mp[nx][ny]==-1)66 mp[nx][ny]=t;67 else68 mp[nx][ny]=min(mp[nx][ny],t);69 }70 }71 cout< <