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
Sample Output
题目大意:在n*m的格子上,#代表能点燃的地方,其余代表不能点燃的地方,最多能使用两个火把点火,燃烧的地方能使它周围的四个方向的格子点燃,每次传递火需要一秒钟,求点燃全部能点燃的地方需要的最短的时间。
思路:因为给的m,n很小,所以直接遍历每两个能点燃的地方;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
char maps[15][15];
int mark[15][15];//用来标记搜过的点
int m,n;//n代表行数,m代表列数
int d[4][2]= {0,1,1,0,0,-1,-1,0};//四个方向
struct p
{
int x,y;
} a,b;
int bfs(p x,p y)//广搜
{
memset(mark,inf,sizeof(mark));
queue<p>q;
mark[x.x][x.y]=0;
mark[y.x][y.y]=0;
q.push(x);
q.push(y);
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0; i<n; i++)
{
b.x=a.x+d[i][0];
b.y=a.y+d[i][1];
if(b.x>=n||b.x<0||b.y>=m||b.y<0)
continue;
if(maps[b.x][b.y]=='#'&&mark[b.x][b.y]>mark[a.x][a.y]+1)
{
mark[b.x][b.y]=mark[a.x][a.y]+1;
q.push(b);
}
}
}
int f=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
if(maps[i][j]=='#')
f=max(f,mark[i][j]);
}
return f;
}
int main()
{
int T,N=0;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
getchar();
int i,j,sum=0,ans=inf;
memset(maps,0,sizeof(maps));
for(i=0; i<n; i++)
{
scanf("%s",maps[i]);
for(j=0; j<m; j++)
if(maps[i][j]=='#')
sum++;
}
if(sum<=2)
{
printf("Case %d:0\n",++N);
continue;
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(maps[i][j]=='#')
{
for(int k=0; k<n; k++)
{
for(int t=0; t<m; t++)
{
if(k<=i&&t<=j)
continue;
if(maps[k][t]=='#')
{
a.x=i;
a.y=j;
b.x=k;
b.y=t;
ans=min(ans,bfs(a,b));
}
}
}
}
}
}
printf("Case %d:",++N);
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}