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.
Input
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
Output
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.
Sample Input
4
3 3
.#.
.#.
3 3
.#.
#.#
.#.
3 3
…
#.#
…
3 3
…#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
枚举两个开始位置,放入队列,进行bfs并记录走过的最远距离。如果能够遍历完所有草,就更新res
最后的三种情况:
1、只有一棵草的情况,特判输出0
2、res=INF,说明无法遍历完所有草,输出-1
3、其他情况,输出res
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
int x1, y1, x2, y2; // 初始的两个点
int t, m, n;
char ma[15][15];
int res, cnt, num; // res表示答案,cnt表示遍历到的草,num表示草的总数
int d[15][15];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int bfs()
{
int step = 0;
// 遍历到的草数目
cnt = 0;
queue<P> que;
que.push(P(x1, y1));
que.push(P(x2, y2));
memset(d, INF, sizeof d);
d[x1][y1] = d[x2][y2] = 0;
while (que.size()) {
P p = que.front(); que.pop();
// 遍历到的草数目加一
cnt++;
for (int i = 0; i < 4; i++) {
int nx = p.first + dx[i], ny = p.second + dy[i];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && ma[nx][ny] == '#' && d[nx][ny] == INF) {
que.push(P(nx, ny));
d[nx][ny] = d[p.first][p.second] + 1;
step = d[nx][ny];
}
}
}
return step;
}
int main(void)
{
cin >> t;
for (int idx = 1; idx <= t; idx++) {
res = INF;
num = 0;
cin >> m >> n;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> ma[i][j];
if (ma[i][j] == '#') {
num++;
}
}
}
for (int a = 0; a < m; a++) {
for (int b = 0; b < n; b++) {
// 遍历当前行剩余的位置
for (int d = b + 1; d < n; d++) {
if (ma[a][b] == '#' && ma[a][d] == '#') {
x1 = a, y1 = b;
x2 = a, y2 = d;
// 走过的最长的步数
int step = bfs();
// 如果烧完的草数目等于所有的草数目
if (cnt == num) {
res = min(res, step);
}
}
}
// 遍历后面的行
for (int c = a + 1; c < m; c++) {
for (int d = 0; d < n; d++) {
if (ma[a][b] == '#' && ma[c][d] == '#') {
x1 = a, y1 = b;
x2 = c, y2 = d;
int step = bfs();
if (cnt == num) {
res = min(res, step);
}
}
}
}
}
}
printf("Case %d: ", idx);
// 特判只有一棵草的情况
if (num == 1) {
cout << 0;
} else if (res == INF) {
cout << -1;
} else {
cout << res;
}
cout << endl;
}
return 0;
}