[FZOJ2150]Fire Game 技巧BFS
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150
题意:两个熊孩子放火烧草坪,每秒会向上下左右四个方向的草坪扩展一格,问选哪两个点可以让草坪最快烧光,如果烧不光要特别说明。
记下所有#的位置,枚举每两个不相同位置的#,跑BFS。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 typedef struct P { 23 int x; 24 int y; 25 int w; 26 P() {} 27 P(int xx, int yy, int ww) : x(xx), y(yy), w(ww) {} 28 }P; 29 30 const int maxn = 15; 31 const int inf = 0x7f7f; 32 const int dx[4] = {0, 0, 1, -1}; 33 const int dy[4] = {1, -1, 0, 0}; 34 int n, m; 35 int ans; 36 bool vis[maxn][maxn]; 37 char G[maxn][maxn]; 38 vector<P> v; 39 40 int bfs(P a, P b) { 41 int cur; 42 queue<P> q; 43 vis[a.x][a.y] = vis[b.x][b.y] = 1; 44 45 q.push(a), q.push(b); 46 while(!q.empty()) { 47 P t = q.front(); q.pop(); 48 for(int i = 0; i < 4; i++) { 49 cur = t.w; 50 int xx = t.x + dx[i]; 51 int yy = t.y + dy[i]; 52 if(xx >= 0 && yy >= 0 && xx < n && yy < m && !vis[xx][yy] && G[xx][yy] == '#') { 53 vis[xx][yy] = 1; 54 q.push(P(xx, yy, t.w+1)); 55 } 56 } 57 } 58 return cur; 59 } 60 61 int main() { 62 // freopen("in", "r", stdin); 63 int T; 64 scanf("%d", &T); 65 for(int _ = 1; _ <= T; _++) { 66 printf("Case %d: ", _); 67 v.clear(); 68 ans = inf; 69 scanf("%d %d", &n, &m); 70 for(int i = 0; i < n; i++) { 71 scanf("%s", G[i]); 72 } 73 for(int i = 0; i < n; i++) { 74 for(int j = 0; j < m; j++) { 75 if(G[i][j] == '#') { 76 v.push_back(P(i, j, 0)); 77 } 78 } 79 } 80 for(int i = 0; i < v.size(); i++) { 81 for(int j = i; j < v.size(); j++) { 82 memset(vis, 0, sizeof(vis)); 83 int t = bfs(v[i], v[j]); 84 int flag = 1; 85 for(int k = 0; k < n; k++) { 86 if(!flag) break; 87 for(int l = 0; l < m; l++) { 88 if(G[k][l] == '#' && !vis[k][l]) { 89 flag = 0; 90 break; 91 } 92 } 93 } 94 if(flag) { 95 ans = min(ans, t); 96 } 97 } 98 } 99 if(ans == inf) { 100 printf("-1\n"); 101 } 102 else { 103 printf("%d\n", ans); 104 } 105 } 106 return 0; 107 }