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
一开始想用dfs,超过三堆就输出-1,两堆就输出较大一堆的最小值,但是发现一堆的时候没法算
比如这种的时候答案是1,dfs做不出来
.##.
.##.
所以用bfs,发现其实bfs也不难哈哈哈,因为只有10*10,所以可以暴力每两个点,O(n^2)复杂度,不会T,记录可以烧光的两个点的时间,输出最小的就好了
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
int n,m,sum;
int dir[4][2]={0,-1,0,1,-1,0,1,0};
int ans=mod,visit[15][15];
struct node
{
int x,y;
int state;
node(int a,int b,int c)
{
x=a;y=b;state=c;
}
node(){
}
};
char map[15][15],a[15][15];
queue<node>v;
bool bfs(node a,node b)
{
if(map[a.x][a.y]=='#')
v.push(a);
if(map[b.x][b.y]=='#')
v.push(b);
if(v.empty()) return false;
node t,tt;
while(!v.empty())
{
t=v.front();
v.pop();
rep(i,0,4)
{
tt.x=t.x+dir[i][0];
tt.y=t.y+dir[i][1];
tt.state=t.state+1;
if(map[tt.x][tt.y]=='#'&&visit[tt.x][tt.y]==0)
{
visit[tt.x][tt.y]=1;
v.push(tt);
sum=max(sum,tt.state);
}
}
}
rep(i,1,n+1)
rep(j,1,m+1)
if(map[i][j]=='#'&&visit[i][j]==0)
return false;
return true;
}
int main()
{
int re;
cin>>re;
rep(m9,1,re+1)
{
ans=mod;
mm(map,'.');
cin>>n>>m;
rep(i,1,n+1)
{
sf("%s",&map[i][1]);
map[i][m+1]='.';
}
int count=0;
mm(a,'.');
rep(i,1,n+1)
rep(j,1,m+1)
{
if(map[i][j]=='#')
count++;
}
if(count<=2)//少于等于两堆一次就烧光了
{
pf("Case %d: 0\n",m9);
continue;
}
rep(i,1,n+1)
{
rep(j,1,m+1)
{
rep(k,1,n+1)
{
rep(l,1,m+1)
{
mm(visit,0);
sum=0;
visit[i][j]=1;visit[k][l]=1;
if(bfs(node(i,j,0),node(k,l,0)))
if(sum)//这个判断似乎多余了,因为bfs可以烧光的话,sum不会等于0,应该可以删了,懒得试,
{
ans=min(ans,sum);
}
}
}
}
}
if(ans==mod)//等于mod的时候说明没有办法烧光
pf("Case %d: -1\n",m9);
else
pf("Case %d: %d\n",m9,ans);
}
return 0;
}