#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;
}
fzu 2150 FireGame
最新推荐文章于 2020-12-04 12:31:45 发布