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
题目大意:有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的。求最少花多少时间可以烧掉,如果烧不掉就输出-1。
思路:枚举所有草地,每次用两个不同的草地进行BFS得出时间,最后取最小的时间即可
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int MAX=10;
const int INF=9999999;
struct node{
int x,y;
int step;
}B[100];
int n,m;
bool visit[MAX][MAX];
char A[MAX][MAX];
int dx[4]={-1,0,0,1},dy[4]={0,1,-1,0};//燃烧的四个方向
int BFS(node t1,node t2){
queue<node> Q;
node tem,temp;
memset(visit,false,sizeof(visit));
Q.push(t1);
Q.push(t2);
visit[t1.x][t1.y]=true;//已访问
visit[t2.x][t2.y]=true;//已访问
while(!Q.empty()){
tem=Q.front();
Q.pop();
for(int i=0 ; i<4 ; i++){
temp.x = tem.x+dx[i];
temp.y = tem.y+dy[i];
//烧四个方向(是草且没被烧过)
if(temp.x>=0 && temp.y>=0 &&temp.x<n && temp.y<m
&& A[temp.x][temp.y]=='#' && !visit[temp.x][temp.y]){
visit[temp.x][temp.y] = true;
temp.step = tem.step+1;
Q.push(temp);
}0322
}
}
//检查草是否烧完
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<m ; j++){
if(A[i][j] == '#' && !visit[i][j]){
return INF;
}
}
}
return temp.step;
}
int main(void){
int N,ans,count;
cin>>N;
for(int _i=1 ; _i<=N ; _i++){
ans = INF;
count=0;
cin>>n>>m;
for(int i=0 ; i<n ; i++){
cin>>A[i];
for(int j=0 ; j<m ; j++){
if(A[i][j] == '#'){
B[count].x = i;
B[count].y = j;
B[count].step=0;
count++;
}
}
}
if(count<=2){
printf("Case %d: 0\n",_i);
continue;
}
//遍历组合所有点
for(int i=0 ; i<count ; i++){
for(int j=i+1 ; j<count ; j++){
int tem = BFS(B[i],B[j]);
if(tem<ans){//烧不完草
ans = tem;
}
}
}
if(ans == INF){
printf("Case %d: %d\n",_i,-1);
}else{
printf("Case %d: %d\n",_i,ans);
}
}
return 0;
}