G - Fire Game FZU - 2150 (kuangbin搜索BFS)

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

简。。简单BFS, 样例给的特别耿直,改了挺久的bug的,挺久没写广搜了犯了很多低级错误,要注意判断是否烧完的时候,需要在最初的时候判断一次,因为可能只有两个草堆。

主要方法就是枚举最开始的两个草堆的位置,逐个bfs找最小步骤。

#include"iostream"
#include"algorithm"
#include"cstring"
#include"queue"
using namespace std;
int n,m,ans,minn,flag;
char s[15][15];
bool vst[15][15];
int dir[4][2]={1,0,-1,0,0,1,0,-1};

struct Point
{
	int x,y;
}a[100];

struct state
{
	int x,y;
	int stp;
};
bool G()
{
	for(int i= 0;i < n;i ++)
	{
		for(int j = 0;j < m;j ++)
		{
			if(s[i][j] == '#')
			{
				if(vst[i][j] == 0)
				{
				//	cout<<"############"<<endl; 
					return 0;
				}
			}
		}
	}

/*	for(int k = 0;k < n;k ++)
	{
		for(int h= 0;h < m;h ++)	
		{
				cout<<vst[k][h]<<" ";
		}
		cout<<endl;
	}
	cout<<"!!!!!!!"<<endl;*/
	return 1;
}
int bfs(state p1,state p2)
{
	queue<state> q;
	p1.stp=0;
	p2.stp=0;
	vst[p1.x][p1.y]=1;
	vst[p2.x][p2.y]=1;
//	cout<<p1.x<<" p1="<<p1.y<<endl;
//	cout<<p2.x<<" p2="<<p2.y<<endl;	
	q.push(p1);
	q.push(p2);
	if(G())
	{
		flag = 1;
		return 0;
	}
	state now,next;
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		for(int i = 0;i < 4;i ++)
		{
			int xx=now.x+dir[i][0];
			int yy=now.y+dir[i][1];
			if(s[xx][yy] == '#' && vst[xx][yy] == 0  && xx>=0 && yy>=0&& xx<n &&yy < m)
			{

				next.x=xx;
				next.y=yy;
				next.stp=now.stp+1;
				
				vst[xx][yy]=1;
				if(G())
				{
					flag = 1;
					return next.stp;
				}
				q.push(next);
			}
		}
		
	}
	return -1;
}
int main()
{
	int t;
	cin >> t;
	int cnt=0;
	while(t--)
	{
		cnt ++ ;
		memset(s,0,sizeof(s));
		
	 	cin >> n >> m;
	 	for(int i = 0;i < n;i ++)
	 	{
	 		cin >> s[i];
	 	}
	 	flag = 0;
	 	int l = 0;
		for(int i = 0;i < n;i ++)
		{
			for(int j = 0;j <m;j ++)
			{
				if(s[i][j] == '#')
				{
					a[l].y=j;
					a[l++].x=i;
				}
			}
		}
		
		 minn=10000000;
		for(int i = 0;i <l;i ++)
		{
			for(int  j = 0;j < l;j ++)
			{
				memset(vst,0,sizeof(vst));
				state p1,p2;
				p1.x=a[i].x;
				p1.y=a[i].y;
				p2.x=a[j].x;
				p2.y=a[j].y;
				
				int h=bfs(p1,p2);
				if(h!=-1)
				{
					minn=min(minn,h);
				}
				
			//	cout<<minn<<endl;
			}
		}
	//	cout<<"f=="<<flag<<endl;
		if(flag == 0)
		{
			minn=-1;
		}
		cout<<"Case "<<cnt<<": "; 
		cout<<minn<<endl;
	}	
	return 0;
}
发布了58 篇原创文章 · 获赞 20 · 访问量 5236

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/82182313