题目描述
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。输出格式具体看样例。
思路:我看数据比较小,就直接暴力枚举了,听说有更加巧妙的做法但是网上搜了一下没找到,如果有哪位dalao知道希望能和我这个蒟蒻说下。
暴力枚举代码如下
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
int dix[4]={0,0,1,-1};
int diy[4]={1,-1,0,0};
char map[15][15];
int vis[15][15];
struct node
{
int x;
int y;
int step;
};
node a[105];
queue<node> que;
int n,m,k;
int minn=1e8;
int bfs()
{
int sum=k-2;
int time;
while(!que.empty())
{
node u=que.front();
que.pop();
time=u.step;
for(int i=0;i<4;i++)
{
int x=u.x+dix[i];
int y=u.y+diy[i];
if(x>=1&&x<=n&&y>=1&&y<=m&&vis[x][y]==0&&map[x][y]=='#')
{
sum--;
vis[x][y]=1;
node v;
v.x=x;
v.y=y;
v.step=u.step+1;
que.push(v);
}
}
}
if(sum==0)
{
return time;
}
else
return -1;
}
int main()
{
int T,t=1;
cin>>T;
while(t<=T)
{
minn=1e8;
cin>>n>>m;
getchar();
k=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='#')
{
a[k].x=i;
a[k].y=j;
a[k].step=0;
k++;
}
}
if(k>2){
for(int i=0;i<k;i++)
for(int j=i+1;j<k;j++)
{
memset(vis,0,sizeof(vis));
while(!que.empty())
que.pop();
vis[a[i].x][a[i].y]=1;
vis[a[j].x][a[j].y]=1;
que.push(a[i]);
que.push(a[j]);
int xiao=bfs();
if(xiao!=-1)
minn=min(minn,xiao);
}
if(minn<1e8)
cout<<"Case "<<t<<": "<<minn<<endl;
else
cout<<"Case "<<t<<": -1"<<endl;
}
else
cout<<"Case "<<t<<": 0"<<endl;
t++;
}
return 0;
}