惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
V
Visual Studio Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
Vercel News
Vercel News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
D
DataBreaches.Net
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
宝玉的分享
宝玉的分享
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
Uva 11624.Fire!|OhYee 博客
2016-09-09 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

Joe works in a maze.
Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan.
Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or horizontally (not diagonally).
The fire spreads all four directions from each square that is on fire.
Joe may exit the maze from any square that borders the edge of the maze.
Neither Joe nor the fire may enter a square that is occupied by a wall.

Input

The first line of input contains a single integer, the number of test cases to follow.
The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000.
The following R lines of the test case each contain one row of the maze.
Each of these lines contains exactly C characters, and each of these characters is one of:
#, a wall
., a passable square
J, Joe’s initial position in the maze, which is a passable square
F, a square that is on fire
There will be exactly one J in each test case.

Output

For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input

2
4 4

#JF#
#..#
#..#
3 3

#J.
#.F

Sample Output

3
IMPOSSIBLE

题解

BFS问题
只是迷宫的地图是动态的,可以在拓展结点的同时,判断是否需要更新迷宫地形,然后采用另一个 BFS 来更新火焰的位置
需要注意的是 火焰起点不止一个

其他就是标准的模板

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

typedef long long LL;

const int INF = 0x7FFFFFFF;
const double eps = 1e-10;

const int maxn = 1005;

const int delta[4] = {1,-1,0,0};

char Map[maxn][maxn];
bool vis[maxn][maxn];
bool fvis[maxn][maxn];
int n,m;

struct Node {
    int x,y,dis;
    Node(int x,int y,int dis) {
        this->x = x;
        this->y = y;
        this->dis = dis;
    }
};

queue<Node> Q;
queue<Node> Qf;

void fire(int d) {
    while(!Qf.empty()) {
        int x = Qf.front().x;
        int y = Qf.front().y;
        int dis = Qf.front().dis;

        if(dis > d) {
            break;
        } else {
            Qf.pop();
        }

        for(int i = 0;i <= 3;i++) {
            int xx = x + delta[i];
            int yy = y + delta[3 - i];

            if(xx<1 || xx>n || yy<1 || yy>m)
                continue;
            if(Map[xx][yy] == '#')
                continue;
            if(fvis[xx][yy])
                continue;

            fvis[xx][yy] = true;
            Map[xx][yy] = 'F';
            
            Qf.push(Node(xx,yy,dis + 1));
        }
    }
}

void Do() {
    int jx,jy;

    cin >> n >> m;

    memset(vis,false,sizeof(vis));
    memset(fvis,false,sizeof(fvis));
    while(!Q.empty())
        Q.pop();
    while(!Qf.empty())
        Qf.pop();

    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= m;j++) {
            cin >> Map[i][j];

            if(Map[i][j] == 'J')
                jx = i,jy = j;
            if(Map[i][j] == 'F') {
                Qf.push(Node(i,j,0));
                fvis[i][j] = true;
            }
        }
    }
    Map[jx][jy] = '.';


    int ans = -1;

    Q.push(Node(jx,jy,0));
    vis[jx][jy] = true;

    int lastdis = -1;

    while(!Q.empty()) {
        int x = Q.front().x;
        int y = Q.front().y;
        int dis = Q.front().dis;
        Q.pop();

        if(dis != lastdis) {
            fire(dis);
            lastdis = dis;
        }

        for(int i = 0;i <= 3;i++) {
            int xx = x + delta[i];
            int yy = y + delta[3 - i];
            int dist = dis + 1;

            if(xx<1 || xx>n || yy<1 || yy>m) {
                ans = dist;
                break;
            }

            if(Map[xx][yy] == '.' && vis[xx][yy] == false) {
                vis[xx][yy] = true;
                Q.push(Node(xx,yy,dist));
            }

        }
        if(ans != -1)
            break;
    }

    if(ans == -1)
        cout << "IMPOSSIBLE" << endl;
    else
        cout << ans << endl;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    int T;
    cin >> T;
    while(T--)
        Do();

    return 0;
}