FZU Problem-2150 Fire Game
FZU入口:http://acm.fzu.edu.cn/problem.php?pid=2150
AC代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f
#define mm(a) memset(a, inf, sizeof(a))
inline const int read(){
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9'){ if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9'){ x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
return x * f;
}
const int MAXN = 15;
struct node{
int x, y;
};
int n, m;
char mp[MAXN][MAXN];
int dis[MAXN][MAXN];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
queue<node> q;
int BFS(int x1, int y1, int x2, int y2){
mm(dis);
node ori1, ori2;
ori1.x = x1; ori1.y = y1;
ori2.x = x2; ori2.y = y2;
dis[x1][y1] = 0; dis[x2][y2] = 0;
q.push(ori1); q.push(ori2);
while (q.size()){
node cur = q.front(); q.pop();
for (int i = 0; i < 4; i++){
int nx = cur.x+dir[i][0];
int ny = cur.y+dir[i][1];
if (nx>=0&&nx<n&&ny>=0&&ny<m&&mp[nx][ny]=='#'&&dis[nx][ny]>dis[cur.x][cur.y]+1){
dis[nx][ny] = dis[cur.x][cur.y]+1;
node nxt;
nxt.x = nx; nxt.y = ny;
q.push(nxt);
}
}
}
int Max = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (mp[i][j] == '#')
Max = max(Max, dis[i][j]);
return Max;
}
int main(){
int t;
t = read();
for (int k = 1; k <= t; k++){
while (q.size()) q.pop();
n = read(); m = read();
for (int i = 0; i < n; i++) scanf("%s", mp[i]);
int ret = inf;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (mp[i][j] == '#'){
for (int ii = 0; ii < n; ii++)
for (int jj = 0; jj < m; jj++)
if (mp[ii][jj] == '#'){
int tmp = BFS(i, j, ii, jj);
ret = min(ret, tmp);
}
}
if (ret == inf) ret = -1;
printf("Case %d: %d\n", k, ret);
}
}