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
解题思路
刚学的队列,输入的时候把稻草点同时放入 结构体数组dot[]中,定义了a,b,两结构体变量,遍历dot[]时,做引火点,放入结构体队列;
队列先进先出,父辈点处理完,才会处理子辈点;
两个引火点,只要开始时一起入列就行了
AC代码
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAX=1000000;
int mov[4][2]={{0,1}, {-1,0}, {0,-1}, {1,0}};
int vis[21][21];
char s[21][21];
int n,m,tot;
struct Node
{
int x,y,t;
};
Node a,b;
int bfs()
{
queue<Node> q;
q.push(a);
q.push(b); //两个点火点入列
vis[a.x][a.y]=1; vis[b.x][b.y]=1;
int time=0;
while(!q.empty())
{
Node next; //可能的下一着火点
Node tmp=q.front();
time=tmp.t; //time记录烧到当前所需时间
for(int i=0; i<4; i++)
{
next.x=tmp.x+mov[i][0];
next.y=tmp.y+mov[i][1];
next.t=tmp.t+1;
if( next.x<n && next.x>=0 && next.y<m && next.y>=0 && !vis[next.x][next.y] && s[next.x][next.y]=='#' )
{
vis[next.x][next.y]=1;
q.push(next); //被传染点入列
}
}
q.pop(); //上一传染点出列
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if( !vis[i][j] && s[i][j]=='#' )
return MAX; //如果存在未访问的稻草点,返回值MAX,否则返回time;
}
}
return time;
}
int main()
{
int T,h,i,j;
scanf("%d",&T);
for(h=1;h<=T;h++)
{
Node dot[151];
scanf("%d%d",&n,&m);
getchar();
tot=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&s[i][j]);
if( s[i][j] == '#')
{
dot[tot].x=i;
dot[tot].y=j;
tot++; //#号点聚集,能遍历选择两着火点
}
}
getchar();
}
if(tot==1 || tot==2)
printf("Case %d: 0\n",h);
else
{
int min=MAX;
for(i=0;i<tot;i++)
{
for(j=i+1;j<tot;j++)
{
memset(vis,0,sizeof(vis));
a=dot[i];
b=dot[j];
a.t=0;
b.t=0;
int time=bfs();
if(min>time)
min=time;
}
}
if(min==MAX)
printf("Case %d: -1\n",h);
else
printf("Case %d: %d\n",h,min);
}
}
return 0;
}