Problem Description
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,计点火的时候时间为0。问点燃一个区域所有草坪所需要的最短时间为多少?如果不能蔓延至所有草坪被点燃,那么输出-1。
题目思路:找两个草坪点(这两个点是两个小孩的起始位置)进行bfs,从这两个点点找,找用时最多的。(为什么?因为这样才能保证草坪可能全部被点燃,然后枚举所有的草坪,找其中时间最少的。)
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef struct Node
{
int x;
int y;
int step;
}node;
node a[105];
node now,nex;
int n,m;
char mmap[15][15];
int vis[15][15];
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
queue<node>q;
int bfs(node a,node b)
{
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
vis[a.x][a.y] = 1;
vis[b.x][b.y] = 1;
q.push(a);
q.push(b);
int This_time = 0;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i = 0;i<4;i++)
{
nex.x = now.x+dir[i][0];
nex.y = now.y+dir[i][1];
if(nex.x>=0 && nex.x<n && nex.y>=0 && nex.y<m && !vis[nex.x][nex.y] && mmap[nex.x][nex.y] == '#')
{
vis[nex.x][nex.y] = 1;
nex.step = now.step+1;
This_time = max(nex.step,This_time);
q.push(nex);
}
}
}
return This_time;
}
bool check()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mmap[i][j] == '#' && vis[i][j] == 0)
{
return false;
}
}
}
return true;
}
int main()
{
int t;
int I;
scanf("%d",&t);
for(I=1;I<=t;I++)
{
memset(mmap,0,sizeof(mmap));
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",mmap[i]);
int k = 0;
for(int i = 0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mmap[i][j] == '#')
{
a[k].x = i;
a[k].y = j;
a[k].step = 0;
k++;
}
}
}
int sum = INF;
for(int i=0;i<k;i++)
{
for(int j=0;j<k;j++)
{
int time = bfs(a[i],a[j]);
if(time<sum && check())
sum = time;
}
}
printf("Case %d: ",I);
if(sum == INF)
printf("-1\n");
else
printf("%d\n",sum);
}
return 0;
}