fzu 2150 FireGame

#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int maxn = 20;
const int INF = 0x3f3f3f3f;
const int xa[5] = { 0,0,1,-1 };
const int ya[5] = { 1,-1,0,0 };

struct node
{
	int x, y;
};
queue<node> q;
int n, m;
char Map[maxn][maxn];
int dis[maxn][maxn];

int bfs(int x1, int y1, int x2, int y2)
{
	while (!q.empty())q.pop();
	memset(dis, INF, sizeof(dis));
	int x, y;
	node p1, p2, now, next;

	p1.x = x1; p1.y = y1;
	p2.x = x2; p2.y = y2;
	dis[x1][y1] = 0;
	dis[x2][y2] = 0;

	q.push(p1); q.push(p2);

	while (!q.empty())
	{
		now = q.front(); q.pop();
		for (int i = 0; i < 4; i++)
		{
			x = now.x + xa[i];
			y = now.y + ya[i];

			if (x >= 0 && x < n
				&& y >= 0 && y < m
				&& Map[x][y] == '#'
				&& dis[x][y]>dis[now.x][now.y] + 1)
			{
				dis[x][y] = dis[now.x][now.y] + 1;
				next.x = x;
				next.y = y;
				q.push(next);
			}
		}
	}

	int Max = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			if (Map[i][j] == '#')
				Max = max(Max, dis[i][j]);//如果有草没烧到则为INF
	return Max;

}


int main()
{
	int T; cin >> T;
	for (int ii = 1; ii <= T; ii++)
	{
		cin >> n >> m;
	/*	for (int i = 0; i <= n; i++)//不可以这样写,因为读入的数据无空格!
			for (int j = 0; j < m; j++)
				cin >> Map[i][j];*/
		for (int i = 0; i < n; i++)
			cin >> Map[i];

		int ans = INF;
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
				if (Map[i][j] == '#')
				{
					for(int k=0;k<n;k++)
						for(int l=0;l<m;l++)
							if (Map[k][l] == '#')
							{
								int t = bfs(i, j, k, l);
								ans = min(ans, t);
							}
				}
		if (ans == INF)ans = -1;//枚举的各个点都存在有草没烧到
		cout << "Case " << ii << ": " << ans << endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值