UVA11624-Fire!(两次bfs)

题目链接


题意:你的任务是帮助J走出一个大火蔓延的迷宫。J每分钟可以超上下左右四个方向移动,而所有着火的格子都会往四周蔓延。迷宫中有一些障碍,J和火都无法进入。当J走到一个迷宫的边界格子时,我们认为他已经走出了迷宫。

思路:这是大白上面的一道题目,其实只要将每个格子什么时间着火处理出来就可以了。两次BFS,第一次处理格子着火的时间,第二次BFS就是判断是否能在最短时间内走出去。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

struct node{
    node (int xx, int yy, int dd) {
        x = xx; 
        y = yy;
        d = dd;
    }
    int x, y, d;
};

char g[MAXN][MAXN];
int vf[MAXN][MAXN], vm[MAXN][MAXN], T[MAXN][MAXN];
int r, c, sx, sy;

queue<node> qf;

void bfs_fire() {
    while (!qf.empty()) {
        node u = qf.front(); 
        qf.pop(); 
        node v = u;
        for (int i = 0; i < 4; i++) {
            int tx = u.x + dx[i]; 
            int ty = u.y + dy[i];      
            if (tx < 0 || tx >= r || ty < 0 || ty >= c || g[tx][ty] == '#') continue;
            if (!vf[tx][ty]) {
                vf[tx][ty] = 1; 
                v.x = tx; 
                v.y = ty;
                v.d = u.d + 1;
                T[tx][ty] = v.d;
                qf.push(v);
            }  
        }
    }
}

int bfs_man() {
    queue<node> qm;
    while (!qm.empty()) qm.pop(); 
    node s(sx, sy, 0);
    qm.push(s);
    memset(vm, 0, sizeof(vm));
    vm[sx][sy] = 1;
    while (!qm.empty()) {
        node u = qm.front(); 
        qm.pop(); 
        if (u.x == 0 || u.x == r - 1 || u.y == 0 || u.y == c - 1)
            return u.d + 1;
        node v = u;
        for (int i = 0; i < 4; i++) {
            int tx = u.x + dx[i]; 
            int ty = u.y + dy[i]; 
            if (tx < 0 || tx >= r || ty < 0 || ty >= c || g[tx][ty] == '#') continue;
            if (u.d + 1 >= T[tx][ty]) continue;
            if (!vm[tx][ty]) {
                vm[tx][ty] = 1; 
                v.x = tx; 
                v.y = ty;
                v.d = u.d + 1;
                qm.push(v);
            }
        }
    }
    return -1;
}

int main() {
    int cas; 
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d", &r, &c);
        for (int i = 0; i < r; i++)
            scanf("%s", g[i]);

        while(!qf.empty())  qf.pop();
        memset(vf, 0, sizeof(vf));
        memset(T, INF, sizeof(T));

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (g[i][j] == 'J') {
                    sx = i; 
                    sy = j; 
                }
                if (g[i][j] == 'F') {
                    node tmp(i, j, 0);       
                    vf[i][j] = 1;
                    T[i][j] = 0;
                    qf.push(tmp);
                } 
            } 
        }

        bfs_fire();
        int ans = bfs_man();    
        if (ans == -1) 
            printf("IMPOSSIBLE\n");
        else 
            printf("%d\n", ans);
    }
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值