Meteor Shower POJ-3669
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
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5
Test instructions
To a meteor shower, give an integer m, indicating that there is M meteor, followed by the M group of data, three positive integers per group of data, the first two represents the meteor falling coordinates (coordinates all in the first quadrant), the third represents the time of the fall, the meteor will set its own coordinates and around four points in total five positions to blow up. Bessie at the time of zero from the coordinates origin, each unit of time can be in the past around her up and down four directions to take a step (in the first quadrant, of course), ask her to reach a safe place (is not to be blown up by Meteor) the shortest time. If you are not able to find a safe place, output "-1".
Problem Solving Ideas:
To find the shortest time, you should think of the breadth of search, the problem is in the basis of broad-based, the map to carry out a mark, marking the earliest time the meteor blew up, the update meteor blew up time, the figure should be the first time the meteor will blow up the area. When you arrive at this point when the meteor has blown up the location, if not blown up, the point to join the queue to continue searching, until the search to a place from the beginning to the end of the meteor is not blown up, it indicates that Bessie reached a safe place.
Attention:
At first, the map should be initialized to initialize the meteor blow up time, but not initialized to 0, because the time is starting from 0, some meteors may be blown up at the time of 0 o'clock. If the coordinates are determined to be out of bounds, the upper limit is not 300, at least 303, because the possible coordinates contain 300 of the meteor will be the coordinates of 301 of the place blown up, Bessie need to go to the coordinates of 302 of the place is considered safe. The spot where the meteor blew up consisted of its own location. There is the possibility that the origin of the coordinates is a safe place, when entering the wide search should be judged alone.
AC Code:
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std
;
int mp[500][500], Minn, flag;
int book[500][500];
int nx[5][2]= {{0,-1},{-1,0},{0,1},{1,0},{0,0}};
struct node//stored in the meteor blew up coordinates, time {int x;
int y;
int temp;
}; void bang (int x, int y, int t)//Mark Meteor blew up coordinates and update the earliest blow-up time {for (int i = 0; i < 5; i++)//Remember that you also need to blow up {i
NT tx = x + nx[i][0];
int ty = y + nx[i][1];
if (tx<0 | | ty<0)//If the judgment cap should be at least 303 continue;
if (mp[tx][ty] = =-1) mp[tx][ty] = t;
else Mp[tx][ty] = min (t,mp[tx][ty]);//Save the earliest time the meteor blew up}} void BFs () {queue<node>q;
Node now, STP; now.x = 0;
The initial coordinates are 0 0 now.y = 0; now.temp = 0;
Time is also starting from 0 Q.push (now); while (!
Q.empty ()) {now = Q.front ();
Q.pop ();
if (mp[now.x][now.y] = =-1) {flag = 1; Minn = now.temp; If the origin is a safe place to return;
Record steps directly return} for (int i = 0; i < 4; i++) {int tx = now.x + nx[i][0];
int ty = now.y + nx[i][1];
if (tx<0 | | ty<0) continue;
if (mp[tx][ty] = = 1)//Find a safe place {flag = 1;
Minn = now.temp + 1;
return;
} if (Now.temp+1 < Mp[tx][ty] &&!book[tx][ty]) {//coordinates that can be walked
Book[tx][ty] = 1;
stp.x = TX;
stp.y = Ty;
Stp.temp = now.temp + 1;
Q.push (STP);
}}}} int main () {int m;
while (~SCANF ("%d", &m)) {int x1, y1, T1;
Memset (Mp,-1,sizeof (MP));
for (int i = 0; i < m; i++) {scanf ("%d%d%d", &x1,&y1,&t1); Bang (X1,Y1,T1);
Flag Meteor Blast } flag = 0;
memset (book,0,sizeof (book));
Book[0][0] = 1;
BFS ();
if (flag) printf ("%d\n", Minn);
else printf (" -1\n");
} return 0;
}