Meteor Shower
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
Source
题目类型:BFS
算法分析:先将每个会被流星雨摧毁的点标记下来,表示这不是终止节点。然后按照流星雨袭击的时间递增排序,每次扩展状态时,要额外更新被摧毁的节点的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 5e4 + 66; const int dx[] = {0, -1, 0, 1}; const int dy[] = {-1, 0, 1, 0}; struct node { int x, y, t; node (int xx, int yy, int tt) : x (xx), y (yy), t (tt) {} node () {} }; bool operator < (const node &a, const node &b) {return a.t < b.t;} int pp; node aa[maxn]; bool vis[366][366], vvis[366][366]; inline bool Ok (int x, int y) { if (x >= 0 && y >= 0 && !vis[x][y]) return true; return false; } void PP (int t) { while (aa[pp].t <= t) { vis[aa[pp].x][aa[pp].y] = true; for (int i = 0; i < 4; i++) { int tx = aa[pp].x + dx[i], ty = aa[pp].y + dy[i]; if (tx >= 0 && ty >= 0) vis[tx][ty] = true; } pp++; } } int bfs (int x, int y, int t) { memset (vis, false, sizeof (vis)); queue <node> qu; qu.push (node (x, y, t)); vis[x][y] = true; pp = 1; PP (0); while (!qu.empty ()) { node temp = qu.front (); qu.pop (); PP (temp.t+1); if (!vvis[temp.x][temp.y]) { return temp.t; } for (int i = 0; i < 4; i++) { int tx = temp.x + dx[i], ty = temp.y + dy[i]; if (Ok (tx, ty)) { qu.push (node (tx, ty, temp.t + 1)); vis[tx][ty] = true; } } } return -1; } int main() { // CFF; int n; while (scanf ("%d", &n) != EOF) { memset (vvis, false, sizeof (vvis)); for (int i = 1; i <= n; i++) { scanf ("%d%d%d", &aa[i].x, &aa[i].y, &aa[i].t); vvis[aa[i].x][aa[i].y] = true; for (int j = 0; j < 4; j++) { int tx = aa[i].x + dx[j], ty = aa[i].y + dy[j]; if (tx >= 0 && ty >= 0) vvis[tx][ty] = true; } } sort (aa + 1, aa + 1 + n); printf ("%d\n", bfs (0, 0, 0)); } return 0; } |
- « 上一篇:poj2431
- poj3050:下一篇 »