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做,只不过一开始push进了两个点,只有把草都烧了才能更新ans。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
const int N=15;
struct node{
int x;
int y;
int step;
};
char a[N][N];
bool st[N][N];
bool vis[N][N];
int n,m,t;
int ans;
queue<node> qq;
node cases[300];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int bfs(int x,int y,int p,int q)
{
memset(st,0,sizeof st);
int re;
node tt1;
tt1.x=x;
tt1.y=y;
tt1.step=0;
qq.push(tt1);
tt1.x=p;
tt1.y=q;
tt1.step=0;
qq.push(tt1);
st[x][y]=1;
st[p][q]=1;
while(qq.size())
{
node t=qq.front();
qq.pop();
re=t.step;
// printf("x=%d,y=%d,p=%d,q=%d,re=%d\n",x,y,p,q,re);
for(int i=0;i<4;i++)
{
int tx=t.x+dx[i];
int ty=t.y+dy[i];
if(tx<0||ty<0||tx>=n||ty>=m) continue;
if(!st[tx][ty]&&a[tx][ty]=='#')
{
node aa;
aa.x=tx;
aa.y=ty;
aa.step=t.step+1;
st[tx][ty]=1;
qq.push(aa);
}
}
}
return re;
}
int main()
{
scanf("%d",&t);
int k=0;
while(t--)
{
ans=1e9;
memset(st,0,sizeof st);
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",a[i]);
int cnt=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(a[i][j]=='#')
{
cases[cnt].x=i;
cases[cnt].y=j;
cnt++;
}
for(int i=0;i<cnt;i++)
for(int j=0;j<cnt;j++)
{
bool flag=1;
bfs(cases[i].x,cases[i].y,cases[j].x,cases[j].y);
for(int ii=0;ii<n;ii++)
{
for(int jj=0;jj<m;jj++)
if(st[ii][jj]==0&&a[ii][jj]=='#')
{flag=0;
break;
}
if(!flag)
break;
}
if(flag)
ans=min(ans,bfs(cases[i].x,cases[i].y,cases[j].x,cases[j].y));
}
if(ans==1e9)
printf("Case %d: -1\n",++k);
else
printf("Case %d: %d\n",++k,ans);
}
}