【题解】FZU-2150 Fire Game

Fire Game (FZU - 2150)

题面

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

提示

思路

暴力枚举两个起火点跑bfs,统计最小值,注意草的数量少于等于2时可以特判

代码

查看代码
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e2+5;

char b[N][N] = {{0}};
bool vis[N][N] = {{0}};
int T, n, m;

int dx[] = { 1, -1, 0, 0};
int dy[] = { 0, 0, 1, -1};

struct P {
int x, y;
int step;
} p[N];

int bfs(P sp1, P sp2) {
memset(vis, 0, sizeof(vis));

queue<P> q;
q.push(sp1);
q.push(sp2);
vis[sp1.x][sp1.y] = 1;
vis[sp2.x][sp2.y] = 1;

int t = 0;

while(!q.empty()) {
P tp = q.front();
q.pop();

t = max(t, tp.step);
P np = tp;

for(int i=0; i<4; i++) {
int x = np.x = tp.x+dx[i];
int y = np.y = tp.y+dy[i];

if(x<0 || x>=n || y<0 || y>=m) {
continue;
}

if(!vis[x][y] && b[x][y]=='#') {
np.step = tp.step+1;
q.push(np);
vis[x][y] = 1;
}
}
}
return t;
}

bool ok(int n, int m) {
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(!vis[i][j] && b[i][j]=='#') {
return 0;
}
}
}
return 1;
}

int main(void) {

scanf("%d", &T);
for(int cs=1; cs<=T; cs++) {
scanf("%d%d", &n, &m);
int num = 0;

for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
scanf(" %c", &b[i][j]);
if(b[i][j]=='#') {
p[num].x = i;
p[num].y = j;
p[num].step = 0;
num++;
}
}
}
printf("Case %d: ", cs);
if(num<=2) {
printf("0\n");
} else {
int mi = inf;
for(int i=0; i<num; i++) {
for(int j=i+1; j<num; j++) {
int t = bfs(p[i], p[j]);
if(ok(n, m))
mi = min(mi, t);
}
}
if(mi==inf)
printf("-1\n");
else
printf("%d\n", mi);
}
}

return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/