题目链接:https://cn.vjudge.net/problem/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

 

题意:

两个人在一个平面草地上某两个点放火,每一单位时间火就向四周四格蔓延(如果是草地的话),求问两个人最少用多少时间能把整片草地烧完。

 

网上题解:

n,m很小,可以暴力枚举两个人的起始放火点,然后BFS。对于每次BFS,返回把这片草地烧完所需的时间。

我的思路:

先求连通块数目cnt,

cnt大于2,输出-1

cnt等于0,输出0

cnt等于1,块内两点bfs

cnt等于2,每个块内进行单点bfs

(具体看代码)

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
const int N=15;
int cnt;
int n,m;
struct node{
	int x,y,temp;
	node(){}
	node(int xx,int yy,int tt){
		x=xx;
		y=yy;
		temp=tt;
	}
};
char mp[N][N];
int t[4][2]={1,0,0,1,0,-1,-1,0};
bool vis[N][N];
void book(int x,int y){
	vis[x][y]=true;
}
void dfs(int x,int y){      //判断有多少个连通块 (把#变成$) 
	mp[x][y]='$';
	for(int i=0;i<4;i++){
		int tx=x+t[i][0];
		int ty=y+t[i][1];
		if(tx<1||tx>n||ty<1||ty>m) continue;
		if(mp[tx][ty]=='#') dfs(tx,ty);
	}
	return ;
}
void dfs(int x,int y,char c){//给每个连通块标号 
	mp[x][y]=c;
	for(int i=0;i<4;i++){
		int tx=x+t[i][0];
		int ty=y+t[i][1];
		if(tx<1||tx>n||ty<1||ty>m) continue;
		
		if(mp[tx][ty]=='$'){
			dfs(tx,ty,c);
		} 
	}
	return ;
}
int bfs(int x,int y){//单点bfs 
	int ans=0;
	memset(vis,0,sizeof(vis));
	queue<node> q;
	q.push(node(x,y,0));
	book(x,y);
	while(!q.empty()){
		node p=q.front();
		ans=max(ans,p.temp);
		q.pop();
		for(int i=0;i<4;i++){
			int tx=p.x+t[i][0];
			int ty=p.y+t[i][1];
			if(tx<1||tx>n||ty<1||ty>m) continue;
			if(vis[tx][ty]==true||mp[tx][ty]=='.') continue;
			book(tx,ty);
			q.push(node(tx,ty,p.temp+1));	
		}
	}
	return ans;
}
int bfs(int x1,int y1,int x2,int y2){//双点bfs 
	int ans=0;
	memset(vis,0,sizeof(vis));
	queue<node> q;
	q.push(node(x1,y1,0));
	q.push(node(x2,y2,0));
	book(x1,y1);
	book(x2,y2);
	while(!q.empty()){
		node p=q.front();
		ans=max(ans,p.temp);
		q.pop();
		for(int i=0;i<4;i++){
			int tx=p.x+t[i][0];
			int ty=p.y+t[i][1];
			if(tx<1||tx>n||ty<1||ty>m) continue;
			if(vis[tx][ty]==true||mp[tx][ty]=='.') continue;
			book(tx,ty);
			q.push(node(tx,ty,p.temp+1));	
		}
	}
	return ans;
}
int main(){
	int T;
	scanf("%d",&T);
	for(int Case=1;Case<=T;Case++){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		scanf("%s",mp[i]+1);
		cnt=0;//连通块数目 
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(mp[i][j]=='#'){
					cnt++;
					dfs(i,j);
				}
			}
		}
		if(cnt>2) printf("Case %d: -1\n",Case);
		else if(cnt==0) printf("Case %d: 0\n",Case);
		else{
			if(cnt==1){  //只有一个连通块,在块内取两个点(可是同一个点)作为起点,遍历所有情况,求最小值 
				int ans=999999999;
				vector<node>v;
				for(int i=1;i<=n;i++){
					for(int j=1;j<=m;j++){
							if(mp[i][j]=='$'){
							v.push_back(node(i,j,0));
						}
					}
				}
				for(int i=0;i<v.size();i++){
					for(int j=i;j<v.size();j++){//不能从i+1枚举,可能只有一个点(特判也行)
						ans=min(ans,bfs(v[i].x,v[i].y,v[j].x,v[j].y));
					}
				}
				printf("Case %d: %d\n",Case,ans);
			}
			else{//两个连通块,一人一个,在块内枚举起点 
				char c='1';
				for(int i=1;i<=n;i++){
					for(int j=1;j<=m;j++){
						if(mp[i][j]=='$'){
							dfs(i,j,c);
						    c++;
						}	
					}
				}
				int ans1=999999999;
				int ans2=999999999;
				for(int i=1;i<=n;i++){
					for(int j=1;j<=m;j++){
						if(mp[i][j]=='1'){
							ans1=min(ans1,bfs(i,j));
						}
						else if(mp[i][j]=='2'){
							ans2=min(ans2,bfs(i,j));
						}
					}
				}
				printf("Case %d: %d\n",Case,max(ans1,ans2));
			}
		}
	}
	return 0;
}