POJ-3669 Meteor Shower---BFS+预处理

题目链接:

https://vjudge.net/problem/POJ-3669

题目大意:

巨大流星雨即将袭来。每个流星会对击中的地方以及周围(上下左右四格)造成破坏。Bessie开始时位于(0, 0)位置,并希望逃到一处不会被袭击到的地方(在第一象限内)。已知每移动一格需要1个时间单位,被流星破坏后的地方不能再进入。给出M个流星在T时刻击中的地方(X, Y),问Bessie能否逃到安全的地方,若能输出最短时间,否则输出-1。

思路:

预处理出每个点的最小爆炸时间,然后从(0,0)开始BFS,注意一个坑点,如果0时刻炸到了0 0 点那就直接输出-1,还有一个坑点就是不能局限于300*300的MAP,300*300的边界会WA,301*301的边界就过了,是因为的流星炸到300*300以内,肯需要逃离到301那一行才行

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<set>
 8 #include<map>
 9 #include<cmath>
10 using namespace std;
11 typedef pair<int, int> Pair;
12 typedef long long ll;
13 const int INF = 0x3f3f3f3f;
14 int T, n, m, d;
15 const int maxn = 1e5 + 10;
16 int Map[405][405];//Map[i][j]表示ij这个点的最短
17 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
18 bool vis[405][405];
19 struct node
20 {
21     int x, y, time;
22     node(int x, int y, int time):x(x), y(y), time(time){}
23     node(){}
24 };
25 int bfs()
26 {
27     queue<node>q;
28     q.push(node(0,0,0));
29     vis[0][0] = 1;
30     while(!q.empty())
31     {
32         node now = q.front();
33         q.pop();
34         //cout<<now.x<<" "<<now.y<<" "<<now.time<<endl;
35         if(Map[now.x][now.y] == INF)
36         {
37             return now.time;
38         }
39         for(int i = 0; i < 4; i++)
40         {
41             int xx = now.x + dir[i][0];
42             int yy = now.y + dir[i][1];
43             node next(xx, yy, now.time + 1);
44             if(xx < 0 || xx > 301 || yy < 0 || yy > 301)continue;
45             if(vis[xx][yy])continue;
46             if(Map[xx][yy] <= next.time)continue;
47             vis[xx][yy] = 1;
48             q.push(next);
49         }
50     }
51     return -1;
52 }
53 int main()
54 {
55     cin >> n;
56     int x, y, w;
57     memset(Map, INF, sizeof(Map));
58     for(int i = 0; i < n; i++)
59     {
60         scanf("%d%d%d", &x, &y, &w);
61         Map[x][y] = min(Map[x][y], w);
62         for(int i = 0; i < 4; i++)
63         {
64             int xx = x + dir[i][0];
65             int yy = y + dir[i][1];
66             if(xx >= 0 && yy >= 0)
67                 Map[xx][yy] = min(Map[xx][yy], w);
68         }
69     }
70     if(Map[0][0] == 0)//特判
71     {
72         cout<<"-1"<<endl;
73         return 0;
74     }
75     cout<<bfs()<<endl;
76     return 0;
77 }

 

posted @ 2018-04-13 16:49  _努力努力再努力x  阅读(148)  评论(0编辑  收藏  举报