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. base_review

    简述Python的字符串驻留机制. - 字符串驻留是一种仅保存一份相同且不可变字符串的方法. - 原理 - 系统维护interned字典,记录已被驻留的字符串对象. - 当字符串对象a需要驻留时,先在 ...

  2. 孤立森林(Isolation Forest)

    前言随着机器学习近年来的流行,尤其是深度学习的火热.机器学习算法在很多领域的应用越来越普遍.最近,我在一家广告公司做广告点击反作弊算法研究工作.想到了异常检测算法,并且上网调研发现有一个算法非常火爆, ...

  3. 20165316 实验一 Java开发环境的熟悉

    实验一 Java开发环境的熟悉 基础-Java环境的构建和简单程序 实验要求 建立"自己学号exp1"的目录 在"自己学号exp1"目录下建立src,bin等目 ...

  4. Django框架----Ajax

    一.Ajax准备知识:json 说起json,我们大家都了解,就是python中的json模块,那么json模块具体是什么呢?那我们现在详细的来说明一下 1.json(Javascript  Obie ...

  5. springboot打war包需要注意事项

    1. pom文件 1.1 添加servlet-api依赖: <!-- 添加servlet-api的依赖--> <dependency> <groupId>org.a ...

  6. 计算概论(A)/基础编程练习(数据成分)/3:整数的个数

    #include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...

  7. shell 冒号

    : ${TEST_LOOP:='1'} 如果不在前面加上:(冒号)命令,那么就会把${TEST_LOOP:='1'}本身当做一个命令来执行,报错是肯定的. [root@node56 ~]# : abc ...

  8. 【react】兄弟组件的通信方式,传统非redux

    很多用过redux开发的朋友们都知道,一般兄弟组件通信可以使用redux. redux也是近期在挤时间学习中.可能也不是很懂,说不定是有错误的理解,若有.现在虽然自己搭建了一个react+router ...

  9. 关于innodb_flush_log_at_trx_commit、innodb_flush_method、innodb_log_block_size和fsync()、O_DIRECT、iops、云盘的关系与总结

    想着整理关于innodb_flush_log_at_trx_commit.innodb_flush_method.innodb_log_block_size和fsync().O_DIRECT.iops ...

  10. The Usage of Lambda and Heap in the C++ STL

    The Usage of Lambda and Heap in the C++ STL Heap In c++ STL, the heap had been implemented as the pr ...