poj Meteor Shower - 搜索

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16313   Accepted: 4291

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 (XiYi) (0 ≤ X≤ 300; 0 ≤ Y≤ 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: XiYi, 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

(转自http://poj.org/problem?id=3669)

  首先呢讲一下题目的大意,Bessie从原点出发,然后有N个流星会在某个时刻落下,它们会破坏
砸到的这个方格还会破坏四边相邻的方块,Bessie不能经过被破坏的方块,问Bessie到一个安全的地
方(不会被任何流星砸到)至少需要多少时间(移动一格的时间为1),如果不可能,则输出-1
  首先呢可以预处理出某个方块被流星砸中的最小时刻,然后bfs,判断是否在这个时刻之前到达这
个方块,如果是安全的,就设成INF。找到了的第一个INF就是答案。
  注意,虽然有时间,但是还是要判重,第一次忘了,然后就TLE。。
 
Code
 1 /*
 2  * poj.org
 3  * Problem#3669
 4  * Accepted
 5  * Time:266ms
 6  * Memory:620k
 7  */
 8 #include<iostream>
 9 #include<cstdio>
10 #include<fstream>
11 #include<cstring>
12 #include<queue>
13 #include<algorithm>
14 #define INF 0xfffffff
15 #define smin(a, b) a = min(a, b);
16 using namespace std;
17 typedef bool boolean;
18 template<typename T>
19 inline void readInteger(T& u){
20     char x;
21     while(!isdigit((x = getchar())));
22     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
23     ungetc(x, stdin);
24 }
25 template<typename T>class Matrix{
26     public:
27         T *p;
28         int lines;
29         int rows;
30         Matrix():p(NULL){    }
31         Matrix(int lines, int rows):lines(lines), rows(rows){
32             p = new T[(lines * rows)];
33         }
34         T* operator [](int pos){
35             return (p + pos * lines);
36         }
37 };
38 int n;
39 Matrix<boolean> visited;
40 Matrix<int> down;
41 inline void init(){
42     visited = Matrix<boolean>(303, 303);
43     down = Matrix<int>(303, 303);
44     fill(down.p, down.p + 303 * 303, INF);
45     memset(visited.p, false, sizeof(boolean) * 303 * 303);
46     readInteger(n);
47     for(int i = 1, a, b, c; i <= n; i++){
48         readInteger(a);
49         readInteger(b);
50         readInteger(c);
51         smin(down[a][b], c);
52         if(a - 1 >= 0)  smin(down[a - 1][b], c);
53         smin(down[a + 1][b], c);
54         if(b - 1 >= 0)    smin(down[a][b - 1], c);
55         smin(down[a][b + 1], c);
56     }
57 }
58 typedef class Point{
59     public:
60         int x;
61         int y;
62         int step;
63         Point(const int x = 0, const int y = 0, const int step = 0):x(x), y(y), step(step){}
64 }Point;
65 int result = -1;
66 queue<Point> que;
67 const int move[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
68 void solve(){
69     if(down[0][0] == INF){
70         result = 0;
71         return;
72     }
73     que.push(Point());
74     visited[0][0] = true;
75     while(!que.empty()){
76         Point e = que.front();
77         que.pop();
78         for(int i = 0; i < 4; i++){
79             Point eu = Point(e.x + move[0][i], e.y + move[1][i], e.step + 1);
80             if(eu.x >= 0 && eu.y >= 0 && eu.step < down[eu.x][eu.y] && !visited[eu.x][eu.y]){
81                 if(down[eu.x][eu.y] == INF){
82                     result = eu.step;
83                     return;
84                 }
85                 visited[eu.x][eu.y] = true;
86                 que.push(eu);
87             }
88         }
89     }
90 }
91 int main(){
92     init();
93     solve();
94     printf("%d", result);
95     return 0;
96 }
posted @ 2016-10-04 19:30  阿波罗2003  阅读(791)  评论(0编辑  收藏  举报