FZU-2150 Fire Game(双BFS)

描述

传送门:FZU-2150 Fire Game text url

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

输入描述

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

输出描述

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

示例

输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#

输出

1
2
3
4
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题解

题目大意

俩小孩放火,只有‘#’格子可燃,两人同时各点燃一个‘#’(可以是同一个),火可以向上向下向左向右在有草的格子蔓延,点火的地方时间为0,蔓延至下一格的时间依次加一,问最快多久能烧完‘#’,如不能烧完则输出“-1”。

思路

依次枚举两个‘#’,然后双路BFS,同时维护最小时间min

代码

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
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define LL long long
const int MAXN = 15, INF = 0x3f3f3f3f;
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
char MAP[MAXN][MAXN];
int vis[MAXN][MAXN], book[MAXN][MAXN];
struct node{ int x, y; }now, nex;

int bfs(int x1, int y1, int x2, int y2){
memset(vis, INF, sizeof(vis));
queue<node> que;
vis[x1][y1] = 0;
vis[x2][y2] = 0;
now.x = x1; now.y = y1;
que.push(now);
now.x = x2; now.y = y2;
que.push(now);
int res = 0;
while(!que.empty()){
now = que.front();
que.pop();
for(int i = 0; i < 4; i++){
int xx = now.x+dx[i], yy = now.y+dy[i];
if(xx >= 0 && yy >= 0 && xx < n && yy < m
&& MAP[xx][yy] == '#' && vis[xx][yy] > vis[now.x][now.y]+1){
vis[xx][yy] = vis[now.x][now.y]+1;
nex.x = xx;
nex.y = yy;
que.push(nex);
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(MAP[i][j] == '#'){
res = max(res, vis[i][j]);
}
}
}
return res;
}

int main(){
int t, ans;
cin >> t;
for(int d = 1; d <= t; d++){
cin >> n >> m;
for(int i = 0; i < n; i ++){
cin >> MAP[i];
}
int temp;
ans = INF;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(MAP[i][j] == '#'){
for(int k = 0; k < n; k++){
for(int g = 0; g < m; g++){
if(MAP[k][g] == '#'){
temp = bfs(i, j, k, g);
ans = min(ans, temp);
}
}
}
}
}
}
if(ans == INF) ans = -1;
cout << "Case " << d << ": " << ans << endl;
}
}