FZU Problem-2150 Fire Game

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值