Fire Game FZU - 2150

Fire Game FZU - 2150

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<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int inf=100000;
int n,m;
char a[15][15];
bool vis[15][15];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

struct node{
	int x,y;//位置 
	int step;//时间 
};

bool judge(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
	{
		return false;
	}
	if(a[x][y]=='.'||vis[x][y]==true)
	{
		return false;
	}
	return true;
}

node bfs(node s1,node s2)
{
	queue<node>q;
	q.push(s1);
	q.push(s2);
	node cur,next;
	vis[s1.x][s1.y]=true;
	vis[s2.x][s2.y]=true;
	int maxn=0;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		maxn=cur.step=max(maxn,cur.step);//因为是从两格草同时开始的,所以最终所用的时间是其中时间最大的 
		for(int i=0;i<4;i++)
		{
			int nx=cur.x+dx[i];
			int ny=cur.y+dy[i];
			if(!judge(nx,ny))
			{
				continue;
			}
			vis[nx][ny]=true;
			next.x=nx;
			next.y=ny;
			next.step=cur.step+1;
			q.push(next);
		}
	}
	return cur;
}

int main()
{
	int t,count=1;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		int i,j,sum=0;
		node s[100];
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cin>>a[i][j];
				if(a[i][j]=='#')//记录草的地方 
				{
					s[sum].x=i;
					s[sum].y=j;
					s[sum].step=0;
					sum++;
				}
			}
		}
		int ans,curent;
		ans=inf;
		if(sum<=2)
		{
			cout<<"Case "<<count<<": "<<0<<endl;
		}
		
		else{
			for(i=0;i<sum;i++)
			{
				for(j=0;j<sum;j++)//任选两个草格开始遍历 
				{
					if(i!=j)
					{
						memset(vis,0,sizeof(vis));
						curent=bfs(s[i],s[j]).step;
						int flag=1;
						for(int k=0;k<n;k++)
						{
							for(int v=0;v<m;v++)
							{
								if(a[k][v]=='#'&&!vis[k][v])
								{
									flag=0;
									break;
								}
							}
						}
						if(flag)
						{
							ans=min(ans,curent);
						}
					}
				}
			}
			
			if(ans!=inf)
			{
				cout<<"Case "<<count<<": "<<ans<<endl;
			}
			else
			{
				cout<<"Case "<<count<<": "<<-1<<endl;
			}
		}
		count++;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ln2037/article/details/88431732