poj3669 Meteor Shower(预处理+bfs)

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

先给地图a[][]预处理每个位置被砸的最小时间。然后再bfs。

纯bfs,还被cin卡了下时间。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<stack>
 8 #define lson l, m, rt<<1
 9 #define rson m+1, r, rt<<1|1
10 #define IO ios::sync_with_stdio(false);cin.tie(0);
11 #define INF 0x3f3f3f3f
12 typedef unsigned long long ll;
13 using namespace std;
14 int a[310][310], vis[310][310];
15 int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
16 typedef struct{
17     int a, b;
18     int step;
19 }Node;
20 Node node;
21 void bfs()
22 {
23     queue<Node> q;
24     node.a = 0; node.b = 0;
25     node.step = 0;
26     q.push(node);
27     vis[0][0] = 1;
28     while(!q.empty()){
29         Node t = q.front(), p;
30         if(a[t.a][t.b] == INF){
31             cout << t.step << endl;
32             break;
33         }
34         for(int i = 0; i < 4; i++){
35             int tx = t.a + dir[i][0];
36             int ty = t.b + dir[i][1];
37             if(tx>=0&&ty>=0&&!vis[tx][ty]){
38                 if(t.step+1<a[tx][ty]){
39                     p.a = tx; p.b = ty;
40                     p.step = t.step+1;
41                     vis[tx][ty] = 1;
42                     q.push(p);
43                 }
44             }
45         }
46         q.pop();
47     }
48     if(q.empty()){
49         cout << "-1" << endl;
50     }
51 }
52 int main()
53 {
54     IO;
55     int m, x, y, t;
56     memset(vis, 0, sizeof(vis));
57     for(int i = 0; i < 310; i++){
58         for(int j = 0; j < 310; j++){
59             a[i][j] = INF;
60         }
61     }
62     cin >> m;
63     for(int i = 0; i < m; i++){
64         cin >> x >> y >> t;
65         a[x][y] = min(a[x][y], t);
66         for(int j = 0; j < 4; j++){
67             int tx = x + dir[j][0]; 
68             int ty = y + dir[j][1];
69             if(tx>=0&&ty>=0){
70                 a[tx][ty] = min(a[tx][ty], t);
71             }
72         }
73     }
74     /*for(int i = 0; i < 10; i++){
75         for(int j = 0; j < 10; j++){
76             cout << a[i][j] << " " ;
77         }
78         cout << endl;
79     }*/
80     bfs();
81     return 0;
82 }
原文地址:https://www.cnblogs.com/Surprisezang/p/8991555.html