POJ 3669
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 can live, if can live, the shortest escape time is how much?
Idea: Sort the meteor shower, then set the value of each point of the map to the earliest time the point was blown up.
#include <iostream> #include <algorithm> #include <queue> #include <cstring>using namespace std ; #define Index_max 512int Map[index_max][index_max];bool visited[index_max][index_max];struct Meteor{int x, y, t;}; typedef METEOR P; Meteor m[50008];int n;const int direction[5][2] = {{-1, 0},{1, 0},{0, 1},{0, 1},{0, 0},};int last;int BFs () {mem Set (visited, 0, sizeof (visited));queue<p> que; P current;current.x = 0;current.y = 0;//currently takes time current.t = 0;que.push (current), while (Que.size ()) {//do a backup const p p = que.f Ront (); Que.pop (); for (int j = 0, J < 4; ++j) {current = P;current.x = current.x + direction[j][0];current.y = current.y + Direc Tion[j][1];++current.t;if (current.x >= 0 && current.y >= 0 && map[current.x][current.y] > Curre NT.T &&!visited[current.x][current.y]) {Visited[current.x][current.y] = true;//explosion time is greater than the current time, is safe if (map[ CURRENT.X][CURRENT.Y] > last) {//The current position exploded more than the meteor shower at the latest time, indicating that the meteor shower area was run out of return current.t;} Que.push (current);}}} return-1;} int main () {cin >> n;for (int i = 0; i < n; ++i) {cin >> m[i].x >> m[i].y >> m[i].t;} The value of each point in the map indicates when the earliest was blown up memset (map, 0x7F, sizeof (map)), for (int i = 0; i < n; ++i) {last = Max (last, m[i].t), and for (int j = 0; J < 5; ++J) {int NX = m[i].x + direction[j][0];int NY = m[i].y + direction[j][1];if (NX >= 0 && ny >= 0 && m Ap[nx][ny] > m[i].t) {map[nx][ny] = m[i].t;}}} if (map[0][0] = = 0) {cout <<-1 << Endl;} Else{cout << BFS () << Endl;} return 0;}
POJ 3669 Meteor Shower "BFS"