POJ3669 Meteor Shower

POJ3669 Meteor Shower

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

1
2
3
4
5
4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

1
5

Answer

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
#include <cstdio>
#include <queue>
using namespace std;

struct P {
int x, y, d;
P() {}
P(int x, int y, int d) : x(x), y(y), d(d) {}
};

const int INF = 10000;

bool vis[402][402];
int ms[402][402];
int dx[5] = {-1, 0, 0, 1, 0};
int dy[5] = {0, 1, -1, 0, 0};

int bfs() {
if (ms[0][0] == INF) return 0;
if (ms[0][0] < 1) return -1;
fill(vis[0], vis[0]+400*400, false);
queue <P> q;
q.push(P(0, 0, 0));
vis[0][0] = true;

while (!q.empty()) {
P p = q.front();
q.pop();
int x = p.x, y = p.y;
for (int i = 0; i < 4; ++i) {
int xx = x + dx[i];
int yy = y + dy[i];
int nd = p.d + 1;
if(xx >= 0 && xx <= 400 && yy >= 0 && y<= 400 && !vis[xx][yy] && ms[xx][yy] > nd) {
if(ms[xx][yy] == INF) {
return nd;
}
q.push(P(xx, yy, nd));
vis[xx][yy] = true;
}
}
}
return -1;
}

void destroy(int x, int y, int t) {
for(int i = 0 ; i < 5; ++i) {
int xx = x + dx[i];
int yy = y + dy[i];
if(xx >= 0 && xx <= 400 && yy >= 0 && y<= 400) {
ms[xx][yy] = min(ms[xx][yy], t);
}
}
}

int solve() {
fill(ms[0], ms[0]+400*400, INF);
int m;
scanf("%d", &m);
while(m--) {
int x, y, t;
scanf("%d %d %d", &x, &y, &t);
destroy(x, y, t);
}
return bfs();
}

int main() {
printf("%d\n", solve());
return 0;
}

Analysis

  1. 给出M次流星的落点和时间,流星会摧毁该点和上下左右四点,矩阵内每点只在摧毁前才可到达。从左上角出发,求能够到达永不会被摧毁的点最短时间。
  2. 预处理出每点最早被摧毁的时间,使用边界和摧毁时间作入队限制,直到队列空或到达不会被摧毁的点。
  3. 注意初始状态可能为被摧毁或永不摧毁。
  4. https://github.com/yogykwan/acm-challenge-workbook/blob/master/src/poj3669.cpp
  5. 题目给的范围是 300,用 301 作为边界反而会出错。。。
# POJ

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×