UVa 11624 Fire!(着火了!)
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!(着火了!)的更多相关文章
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- UVA 11624 Fire! bfs 难度:0
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVA 11624 Fire!
题目大意: F代表火焰 J代表人 一个人想在火焰烧到自己之前逃出着火地区 . 为路,人可以走,火可以燃烧(当然如果火先烧到就不能走了) #为墙,不可走 如果能逃出,输出时间,不能,输出IMPOSSIB ...
- UVA 11624 Fire!(广度优先搜索)
题目大意:在一个N*M的迷宫内,J代表某人(只有一个),F代表火(可能不只一个),#代表墙,火每分钟会向四周除了墙以外的地方扩散一层,问人能否在没被火烧到 之前逃出迷宫,若能逃出输出最短时间.很明显的 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- uva 11624 Fire!(搜索)
开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...
随机推荐
- Spring源码阅读(三)
上一讲我们谈到单例生产关键方法getSingleton.getSingleton方法由DefaultSingletonBeanRegistry类实现.我们的抽象工厂AbstractBeanFactor ...
- C# 复制值类型的变量和类
C#大多数基元类型包括int.float.double.和char等,注意这里不包括string,这些都是值类型.将变量声明为值类型,编译器会生成代码来分配足以容纳这个值得内存块.编译器分配内存的时候 ...
- 51Nod 1212 无向图最小生成树 (路径压缩)
N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. Input 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N <= 1000, 1 &l ...
- [转载]decode()函数简介
今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即 ...
- MVC 部署HTTPS网站
一.项目配置 什么是全站HTTPS 全站HTTPS就是指整个网站的所有页面,所有资源全部使用HTTPS链接.当用户的某个请求是明文的HTTP时,应该通过HTTP状态码301永久重定向到对应的HTTPS ...
- django自定义错误响应
在做一个web时,总是会出现各种错误,如400.403.404.500等.一般开发都要做对应的处理,给一些友好提示,或返回一些公益广告等. 在Django中,默认提供了常见的错误处理方式,比如: ha ...
- 01:CENTOS使用VIRTUALENV搭建独立的PYTHON环境-PYTHON虚拟环境
1.1 安装virtualenv环境 https://www.cnblogs.com/liuyansheng/p/6141197.html 1.安装virtualenv yum install pyt ...
- Linux下Keepalived安装与配置
一.简介 负载平衡是一种在真实服务器集群中分配IP流量的方法,可提供一个或多个高度可用的虚拟服务.在设计负载均衡拓扑时,重要的是要考虑负载均衡器本身的可用性以及它背后的真实服务器.用C编写的类似于la ...
- python简说(七)元组,集合
一.元组 元组也是一个list,但是它的值不能改变 定义元组的时候,只有一个元素,后面得加逗号 oracle_info = (123,) 二.集合 1.集合天生就可以去重,集合是无序的 2.#交集 r ...
- js的匿名函数 和普通函数
匿名函数在声明时不用带上函数名, 可以把匿名函数当作一个function类型的值来对待 声明一个普通的函数 function func() { ... } 可以认为和var func = functi ...