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.
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 题意,从两个地方点火,看看烧完最短的时间是多少,火不能烧'.',只能烧'#',也就是遇到了句点,火焰停止蔓延;
一开始想看看有没有啥规律,想判断如果连通分支超过两个就不行,然后分开讨论一个连通分支与两个连通分支的情况,可是规律不通用,于是选择了排着遍历,mint给一个很大的值,每次选两个地方进行点火,然后都入队,bfs,如果能把草全烧完,更新最小时间,最后如果最小时间还是原来很大的值,输出-1.
但是忘记了还有只有一块草的情况,那就另外加了一下就过了。 代码:
#include <iostream>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std; int dir[][]={,,,,,-,-,};
int T,n,m,x,y,t,vis[][],c;
char mp[][];
class que
{
public:
int x,y,time;
}temp;
int xx[],yy[],xy,mint;
int main()
{
cin>>T;
for(int l=;l<=T;l++)
{
mint=;//updated
cin>>n>>m;
queue<que>q;
xy=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='#')
xx[xy]=i,yy[xy]=j,xy++;
}
for(int i=;i<xy;i++)
{
for(int j=i+;j<xy;j++)
{
memset(vis,,sizeof(vis));//initialized
temp.x=xx[i],temp.y=yy[i],temp.time=;
q.push(temp);
vis[temp.x][temp.y]=;
temp.x=xx[j],temp.y=yy[j],temp.time=;
q.push(temp);
vis[temp.x][temp.y]=;
c=;//updated
t=;//updated
while(!q.empty())
{
if(t<q.front().time)t=q.front().time;
for(int k=;k<;k++)
{
x=q.front().x+dir[k][];
y=q.front().y+dir[k][];
if(x<||y<||x>=n||y>=m||mp[x][y]=='.'||vis[x][y])continue;
temp.x=x,temp.y=y,temp.time=q.front().time+;
q.push(temp);
vis[x][y]=;
c++;
}
q.pop();
}
if(c==xy){if(t<mint)mint=t;}//judged
} }
printf("Case %d: ",l);
if(mint<)cout<<mint<<endl;
else if(xy==)cout<<<<endl;//special
else cout<<-<<endl; }
}