Meteor Shower
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31358 | Accepted: 8064 |
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 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
Meaning of the questions: Bessie starting from the origin, then there are N meteors will fall at some point, they will destroy the hit of the grid will destroy
Adjacent to the four sides of the box, after how much time he can be output to a safe place. If possible, the output of the optimal solution, it is impossible to output 1.
#include <the iostream> #include < String .h> #include < String > #include <algorithm> #include <Queue> the using namespace STD; int the dir [ . 5 ] [ 2 ] = {{ 0 , 0 }, { 0 , - 1 }, { 0 , 1 }, { 1 , 0 }, {- 1 , 0 }}; // origin stays (explosion process), up and down int A [ 505 ] [ 505 ]; int n,m,cnt; struct node { int x; int y; int t; }temp,now; int check(int x,int y) { if(x>=0&&x<500&&y>=0&&y<500) return 1; else return 0; } int bfs() { if(a[0][0]==0)//刚开始走就被炸死了 return -1 ; IF (A [ 0 ] [ 0 ] == - 1 ) // starting point is a safe place, do not go return 0 ; temp.x = 0 , temp.y = 0 , temp.t = 0 ; // starting Queue <Node> P; p.push (TEMP); the while (! p.empty ()) { now = p.front (); p.pop (); for ( int I = . 1 ; I < . 5 ; I ++ ) // can not stay in place { intDX, Dy, dt; DX = + now.x the dir [I] [ 0 ]; Dy = + now.y the dir [I] [ . 1 ]; dt = now.t + . 1 ; IF (Check (DX, Dy) = = 0 ) // out boundary Continue ; IF (A [DX] [Dy] == - . 1 ) // to safety zone return dt; IF (dt> = A [DX] [Dy]) // into the explosive region Continue ; A [DX] [Dy] = dt; // update temp.x = DX; temp.y= Dy; temp.t = dt; p.push (TEMP); // point of the next search into queue } } return - . 1 ; // not to safe areas } // BFS from the nearest point to start the search, so the first answer is to get the nearest int main () { cin >> the n-; memset (a, - 1 , sizeof (a)); // initialize the map all places you can go the while (n-- ) { int the X-, Y, T; CIN >> >> X Y >> T; for ( int= I 0 ; I < . 5 ; I ++) // pretreated map after the end of all explosives { int DX, Dy; DX = X + the dir [I] [ 0 ]; Dy = Y + the dir [I] [ . 1 ]; IF (Check (DX, Dy) == 0 ) // beyond the range of the map Continue ; IF (A [DX] [Dy] == - . 1 ) A [DX] [Dy] = T; the else // IF (A [ DX] [Dy] = -. 1)! a [DX] [Dy] = min (a [DX] [Dy], T); // get the first explosion time } } cout<<bfs()<<endl; return 0; }