FZU - 2150 Fire Game

本文介绍了一种基于矩阵的草火蔓延算法,通过枚举所有可能的起点并使用广度优先搜索来确定点燃所有草地所需的最短时间。文章详细解释了算法的实现过程,包括如何处理边界条件和特殊情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

思路:枚举所有可能开始的两个点,选取结果的最小值,如果不能烧完,则输出-1;

坑:题目再visualc++上提交,与devc++的用法有些还是不一样,结构体不能直接在{}中写上,min与max函数不能使用,

标准库在<algorithm>头中定义了两个模板函数std::min() 和 std::max()。通常用它可以计算某个值对的最小值和最大值。

可惜在 Visual C++ 无法使用它们,因为没有定义这些函数模板。原因是名字min和max与<windows.h>中传统的min/max宏定义有冲突。为了解决这个问题,Visual C++ 定义了另外两个功能相同的模板:_cpp_min() 和 _cpp_max()。我们可以用它们来代替std::min() 和 std::max()。

为了禁用Visual C++中的 min/max宏定义,可以在包含<windows.h>头文件之前加上:

我们也可以自己定义此函数

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<queue>
#include<cmath>
#include<math.h>
#include<algorithm>
using namespace std;
int minn(int x,int y){
	if(x<=y)
		return x;
    else
		return y;
}
int mov[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char c[15][15];
struct node{
	int x,y,tt;
	node(){	}
	node(int x1,int y1,int t1){
		x=x1;
		y=y1;
		tt=t1;
	}
}nd[110];
int vst[15][15];
queue<node> q;
int t,n,m,k;//k用来记录有多少个草地 
int maxx;
int bfs(int i,int j){
	while(!q.empty()) q.pop();
	int sum=0;
	memset(vst,0,sizeof(vst));
	maxx=0;
	node a=nd[i];
	node b=nd[j];
	q.push(a);
	sum++;
	q.push(b);
	sum++;
	vst[a.x][a.y]=1;
	vst[b.x][b.y]=1;
	while(!q.empty()){
		if(sum==k) break;
		a=q.front();
		q.pop();
		int xx,yy;
		for(int i=0;i<4;i++){
			for(int j=0;j<2;j++){
				xx=a.x+mov[i][0];
				yy=a.y+mov[i][1];
				if(c[xx][yy]=='#'&&!vst[xx][yy]&&xx>=1&&xx<=n&&yy>=1&&yy<=m){
					vst[xx][yy]=1;
					node g;
					g.x=xx;
					g.y=yy;
					g.tt=a.tt+1;
					q.push(g);
					sum++;
					if(a.tt+1>maxx){
						maxx=a.tt+1;
					}
				}
			}
		}
	}
	if(sum<k)
	   maxx=-1;
	return maxx;
}
void main(){
	scanf("%d",&t);
	for(int h=1;h<=t;h++){
		scanf("%d%d",&n,&m);
		k=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>c[i][j];
				if(c[i][j]=='#'){
					++k;
					nd[k].x=i;
					nd[k].y=j;
					nd[k].tt=0;
				}
			}
		}
		if(k==1){
			cout<<"Case "<<h<<":"<<" "<<"0"<<endl;
		}else{
		int flag=0;
		int res=0x3f3f3f3f;
		for(int i=1;i<k;i++){
			for(int j=i+1;j<=k;j++){
				int v=bfs(i,j); 
			    if(v>=0){
			    	flag=1;
			    	res=minn(res,v);
				}
			}
		}
		if(!flag){
			cout<<"Case "<<h<<":"<<" "<<"-1"<<endl;
		}else{
			cout<<"Case "<<h<<":"<<" "<<res<<endl;
		}
	}
	}
	return;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值