BFS廣度優先搜尋(10)--fzu2150(基礎題)

Sly_461發表於2016-09-25
Fire Game

                                    Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u


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


            這道題就是說任選兩個是‘#’的格子(‘#‘表示此格子是能夠燃燒的草,‘.’ 表示是不能燃燒的石頭),同時點燃,一個格子的火一秒鐘能夠將它上下左右的是草的格子都燃燒掉,問最少經過多少秒將所有是草的格子燃燒完,若不能燃燒完,輸出-1。

        解題思路很簡單,就是分別列舉兩個不相同的格子選中點燃(若只有一個或者兩個格子,則直接輸出0),列舉完所有情況後再比較輸出所用最少的時間。程式碼如下:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char map[12][12];
int vis[12][12];         
int d[4][2]={0,1,1,0,0,-1,-1,0};  //上下左右四個方向
int n,m;
struct node{
	int x,y;
	int step;
};
int fire[12][12];      //已經燃起火的格子為1
node f[100];
int cont;        //是草的格子的數量
int Bfs(node s,node s1){     
	queue<node>q;
	node e;
	int i;
	memset(fire,0,sizeof(fire));
	fire[s.x][s.y]=1;
	fire[s1.x][s1.y]=1;
	q.push(s);
	q.push(s1);
	int num=2;
	while(!q.empty())
	{
		s=q.front();
		q.pop();
		for(i=0;i<4;i++){
			int xx=s.x+d[i][0];
			int yy=s.y+d[i][1];
			if(xx<0||yy<0||xx>=n||yy>=m)
				continue;
			if(map[xx][yy]=='.')continue;
			if(fire[xx][yy])continue;
			fire[xx][yy]=1;
			num++;
			e.x=xx;
			e.y=yy;
			e.step=s.step+1;
			q.push(e);
		}
		if(q.empty()&&num==cont)return s.step;     //所有為草的格子都被燃燒完了
	}
	return -1;       //有為草的格子沒有燃燒完
}
int main()
{
	int T,i,j;
	int cnt=0;
	scanf("%d",&T);
	while(T--){
		scanf("%d %d",&n,&m);
		memset(f,0,sizeof(f));
		for(i=0;i<n;i++){
			scanf("%s",map[i]);
		}
		cont=0;
		for(i=0;i<n;i++){                //任選兩個是草的格子為起點開始Bfs
			for(j=0;j<m;j++){
				if(map[i][j]=='#'){
					node a;
					a.x=i;
					a.y=j;
					a.step=0;
					f[cont++]=a;
				}
			}
		}
		if(cont==1||cont==2){        //有草的格子只有一個或者兩個
			printf("Case %d: 0\n",++cnt);
			continue;
		}
		int ans=105;
		for(i=0;i<cont;i++){               //找出所用時間最小的
			for(j=i+1;j<cont;j++){
				int ans1=Bfs(f[i],f[j]);
				if(ans1==-1)
				{
					if(ans==105)ans=-1;
				}
				else{
					if(ans==-1)ans=ans1;
					else ans=(ans<ans1)?ans:ans1;
				}
			}
		}
		printf("Case %d: %d\n",++cnt,ans);
	}
	return 0;
}


相關文章