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 <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
using namespace std;
char maap[15][15];
struct node
{
int x,y,step;
}h[105];
int num[15][15],c,d;
int dsd[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int pan()
{
int i,j;
for(i=1;i<=c;i++)
{
for(j=1;j<=d;j++)
{
if(maap[i][j]=='#'&&num[i][j]==0)
return 0;
}
}
return 1;
}
int bfs(struct node s,struct node p)
{
queue<node>Q;
Q.push(s);
Q.push(p);
num[s.x][s.y]=num[p.x][p.y]=1;
while(Q.size())
{
s=Q.front();
Q.pop();
int t;
for(t=0;t<4;t++)
{
p=s;
p.x+=dsd[t][0];
p.y+=dsd[t][1];
if(p.x>=1&&p.x<=c&&p.y>=1&&p.y<=d&&num[p.x][p.y]==0&&maap[p.x][p.y]=='#')
{
p.step=s.step+1;
Q.push(p);
num[p.x][p.y]=1;
}
}
}
return s.step;
}
int main()
{
int a,b,e,f,g,tt,ss;
scanf("%d",&a);
for(b=1;b<=a;b++)
{
scanf("%d %d",&c,&d);
getchar();
g=0;
for(e=1;e<=c;e++)
{
for(f=1;f<=d;f++)
{
scanf("%c",&maap[e][f]);
if(maap[e][f]=='#')
{
h[++g].x=e;
h[g].y=f;
h[g].step=0;
}
}
getchar();
}
ss=9999;
for(e=1;e<=g;e++)
{
for(f=e;f<=g;f++)
{
memset(num,0,sizeof(num));
tt=bfs(h[e],h[f]);
if(ss>tt&&pan())
{
ss=tt;
}
}
}
if(ss==9999)
printf("Case %d: %d\n",b,-1);
else printf("Case %d: %d\n",b,ss);
}
return 0;
}