Fire Game FZU - 2150

K - Fire Game

FZU - 2150

题意:两个孩子在两块草地放火, 火向四周蔓延, 问最短多长时间将草地烧净, 若能输出最小值, 反之输出-1;
思路:两个点同时bfs
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
int n, m;
char maze[15][15];
int nex[4][2]={1, 0, 0, 1, -1, 0, 0, -1};
int cnt=0, vis[15][15];
struct node{
	int x, y;
};
queue<node> q;
int t[15][15];
int bfs(int x, int y, int a, int b){
	while(!q.empty()) q.pop();
	node tmp;
	tmp.x=x;
	tmp.y=y;
	q.push(tmp);
	tmp.x=a;
	tmp.y=b;
	q.push(tmp);
	memset(t, INF, sizeof(t));
	t[x][y]=t[a][b]=0;
	int maxn=0;
	while(!q.empty()){
		tmp=q.front();
		q.pop();
		for(int i=0; i<4; i++){
			int tx=tmp.x+nex[i][0];
			int ty=tmp.y+nex[i][1];
			if(tx<0||ty<0||tx>=n||ty>=m||maze[tx][ty]=='.'||t[tx][ty]<=t[tmp.x][tmp.y]) continue;
			node tt;
			tt.x=tx;
			tt.y=ty;
			t[tx][ty]=t[tmp.x][tmp.y]+1;
			q.push(tt);
		}
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			if(maze[i][j]=='#') maxn=max(maxn, t[i][j]);
		}
	}
	return maxn;
}
int main(){
	int T, ca=0;
	scanf("%d", &T);
	while(T--){
		ca++;
		memset(vis, 0, sizeof(vis));
		scanf("%d%d", &n, &m);
		getchar();
		for(int i=0; i<n; i++){
			scanf("%s", maze[i]);
		}
		int ans=INF;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				if(maze[i][j]=='#'){
					for(int k=0; k<n; k++){
						for(int l=0; l<m; l++){
							if(maze[k][l]=='#'){
								ans=min(ans, bfs(i, j, k, l));
							}
						}
					}
				}	
			}
		}
		if(ans==INF) ans=-1;
		printf("Case %d: %d\n", ca, ans);
	}	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值