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
代码:
///数据量比较小,所以暴力+枚举就行。。。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
struct node
{
int x;
int y;
int step;
} p[225];
int n,m;
char mp[15][15];
bool vis[15][15];
int walk[][2]= {{-1,0},{0,1},{1,0},{0,-1}};///走的方向
int judge()
{
int i,j;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(mp[i][j]=='#'&&vis[i][j]==0)
return 0;
}
}
return 1;
}
int bfs(struct node a,struct node b)
{
queue<struct node>q;
q.push(a);///和普通的bfs不同的是这里上来就会压入2个数据。
q.push(b);
vis[a.x][a.y] = 1;
vis[b.x][b.y] = 1;
int mx = 0;
while(!q.empty())
{
a = q.front();
q.pop();
for(int i=0; i<4; i++)
{
b.x = a.x + walk[i][0];
b.y = a.y + walk[i][1];
if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&vis[b.x][b.y]==0&&mp[b.x][b.y]=='#')
{
vis[b.x][b.y] = 1;
b.step = a.step + 1;
if(mx<b.step)
mx = b.step;
q.push(b);
}
}
}
return mx;///返回最大值。
}
int main()
{
int T;
int t = 0;
scanf("%d",&T);
while(T--)
{
t++;
scanf("%d %d",&n,&m);
int cnt = 0;
for(int i=0; i<n; i++)
{
scanf("%s",mp[i]);
for(int j=0; j<m; j++)
{
if(mp[i][j]=='#')///把每个点都初始化。用于后面的暴力。
{
p[cnt].x = i;
p[cnt].y = j;
p[cnt].step = 0;
cnt++;///记录草坪的数量。可以对下面进行优化。
}
}
}
int step;
int sum = INF;
for(int i=0; i<cnt; i++)
{
for(int j=i; j<cnt; j++)
{
memset(vis,0,sizeof(vis));///因为是暴力,所以每一次都要重新更新。
step = bfs(p[i],p[j]);///返回最大值。(两步的)
int temp = judge();///判断一下。是否全部点燃草坪。没有的话就不会更新sum。(这里很关键)
if(step<sum&&temp)///sum取最小值。
sum = step;///也就是说,如果更新的话,这个矩阵最多有两块草坪。(如果有两块草坪,并且分别在两块草坪上点火,那么就会燃烧完,只不过就是时间长短的问题,所以上面函数的mx就是返回每一次暴力的最大值(这里是最大值,不用说也知道,除非你没有读懂题。),所以把每一次记录的最大值进行比较,找到最小的,就是最优的。)
}
}
if(sum==INF)
printf("Case %d: -1\n",t);
else
printf("Case %d: %d\n",t,sum);
}
return 0;
}