UVa 11624 - Fire!(着火了!)

Time limit: 1.000 seconds


Description - 题目描述

  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.

Joe在一个迷宫里工作。部分迷宫不幸起火,然而迷宫主人并没有制定逃生计划。帮帮Joe逃出生天吧。

给定Joe与迷宫起火部分的位置,你需要确定Joe能否在被火上身前逃出迷宫,以及最快出逃时间。

Joe与火焰每分钟都能垂直或水平(不能斜向)移动一个格子。每个着火的格子都会向全部四个方向传播火焰。Joe可以从任意迷宫边缘的格子逃出去。Joe和火焰都无法穿墙。

CN

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.

输入的第一行为一个整数,表示测试用例的数量。
每组测试用例的第一行有两个整数R和C,以空格隔开,且1 ≤ R; C ≤ 。
随后R行,每行表示其中迷宫中的其中一行。每行C个字符,各字符如下: • #, 墙
• ., 可通行的格子
• J, Joe的初始位置, 在一块可通行格子上
• F, 火焰所在的格子 每组测试用例只有一个J。

CN

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.

对于每组测试用例,如果Joe无法火焰烧到他前逃出则输出‘IMPOSSIBLE’,否则输出一个整数表示Joe逃出迷宫的最早时间,单位分钟。

CN

Sample Input - 输入样例

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Sample Output - 输出样例

3
IMPOSSIBLE

题解

  一般BFS
  可以把火和人分别拆成2个BFS。
  或二者合并,先搜索火焰传播,在搜索人是否可以移动。

代码 C++

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define INF 0x7F7F7F7F
#define MX 1005
struct Point {
int y, x;
}J, now, nxt;
std::queue<Point> q;
int data[MX][MX], fx[] = { , , -, , , , , - }, y, x, opt;
char map[MX][MX];
void BFS() {
int i, tmp;
q.push(J);
while (!q.empty()) {
now = q.front(); q.pop(); tmp = data[now.y][now.x] + ;
if (now.y == || now.y == y || now.x == || now.x == x) {
if (map[now.y][now.x] == 'J') { opt = std::min(opt, tmp); continue; }
}
for (i = ; i < ; i += ) {
nxt = { now.y + fx[i], now.x + fx[i + ] };
if (map[nxt.y][nxt.x] == '.' && tmp < data[nxt.y][nxt.x]) {
map[nxt.y][nxt.x] = map[now.y][now.x]; data[nxt.y][nxt.x] = tmp;
q.push(nxt);
}
}
}
}
int main() {
int t, i, j;
scanf("%d", &t);
while (t--) {
memset(map, , sizeof map); memset(data, INF, sizeof data);
opt = INF;
scanf("%d%d ", &y, &x);
for (i = ; i <= y; ++i) gets(&map[i][]);
for (i = ; i <= y; ++i) for (j = ; j <= x; ++j) {
switch (map[i][j]) {
case 'J': J = { i, j }; data[i][j] = ; break;
case 'F': q.push({ i, j }); data[i][j] = ; break;
}
}
BFS();
if (opt == INF) puts("IMPOSSIBLE");
else printf("%d\n", opt);
}
return ;
}

UVa 11624 Fire!(着火了!)的更多相关文章

  1. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  2. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  3. UVA 11624 Fire! bfs 难度:0

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  6. UVA 11624 Fire!

    题目大意: F代表火焰 J代表人 一个人想在火焰烧到自己之前逃出着火地区 . 为路,人可以走,火可以燃烧(当然如果火先烧到就不能走了) #为墙,不可走 如果能逃出,输出时间,不能,输出IMPOSSIB ...

  7. UVA 11624 Fire!(广度优先搜索)

    题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...

  8. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  9. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

随机推荐

  1. python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出

    想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数.它会使得输出和pprint() 函数效果类似 >>> data {'age': 4, 'nam ...

  2. LDA的Gibbs Sampling求解

    <LDA数学八卦>对于LDA的Gibbs Sampling求解讲得很详细,在此不在重复在轮子,直接贴上该文这部分内容. Gibbs Sampling 批注: 1.              ...

  3. maven的profile详解

    详细内容请见:https://www.cnblogs.com/wxgblogs/p/6696229.html Profile能让你为一个特殊的环境自定义一个特殊的构建:profile使得不同环境间构建 ...

  4. Druid-目前最好的连接池

    https://blog.csdn.net/youanyyou/article/details/78992979 Druid是什么Druid是阿里开源的连接池,是Java语言中最好的数据库连接池.Dr ...

  5. 垂直打击之JVM剖析

    让Java应用程序运行是一回事,但让他们跑得快就是另外一回事了.在面对对象的环境中,性能问题就像来势凶猛的野兽.但JVM的复杂性将性能调整的复杂程度增加了一个级别.这里Refcard涵盖了JVM in ...

  6. Cookie,Session,正则表达式

    一.Cookie和Session基础知识 Cookie:客户端本地存储的键值对 Http访问是不记录状态的,所以要借助session和cookie来保存访问状态  当你在浏览网站的时候,WEB 服务器 ...

  7. 网上搜到的权限系统demo

    网上搜到的权限系统demo http://www.sojson.com/shiro

  8. python pip

    如果pip的版本较低,可能导致pip时安装出错,所以我们要更新pip版本-- 查询pip版本 pip -V -- Linux and OS X 升级 pip install -U pip -- Win ...

  9. MongoDB入门一

    一.环境配置 1.下载MongoDB,找到Bin目录下所有的.exe文件,拷贝到G盘MongoDB(新建)下,在MongoDB下建一个data文件,用于存放数据,创建一个logs文件夹,文件夹下创建一 ...

  10. 模拟实现ATM+购物商城程序

    流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...