#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct node
{
int x, y, step;
} a[105], temp, key;
char mp[12][12];
bool vis[12][12];
int to[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int main()
{
int t;
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++)
{
int n, m;
int cnt = 0;
int MIN = 1000;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
scanf("%c", &mp[i][j]);
if (mp[i][j] == '#')
{
a[cnt].x = i;
a[cnt].y = j;
a[cnt++].step = 0;
}
}
}
for (int i = 0; i < cnt; i++)
{
for (int j = i; j < cnt; j++)
{
int MAX = 0;
memset(vis, 0, sizeof(vis));
queue<node> q;
q.push(a[i]);
vis[a[i].x][a[i].y] = true;
q.push(a[j]);
vis[a[j].x][a[j].y] = true;
while (!q.empty())
{
key = q.front();
q.pop();
for (int o = 0; o < 4; o++)
{
temp.x = key.x + to[o][0];
temp.y = key.y + to[o][1];
if (temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m && mp[temp.x][temp.y] == '#' && vis[temp.x][temp.y] == false)
{
temp.step = key.step + 1;
MAX = max(temp.step, MAX);
q.push(temp);
vis[temp.x][temp.y] = true;
}
}
}
bool flag = false;
for (int l = 0; l < n; l++)
{
for (int r = 0; r < m; r++)
{
if (mp[l][r] == '#' && vis[l][r] == false)
{
flag = true;
}
if (flag == true)
{
break;
}
}
if (flag == true)
{
break;
}
}
if (flag == true)
{
continue;
}
else
{
MIN = min(MAX, MIN);
}
}
}
if (MIN == 1000)
MIN = -1;
printf("Case %d: %d\n", cas, MIN);
}
return 0;
}
FZU - 2150 Fire Game 簡單搜索
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.