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[][]是多余的,完全可以用 ...
随机推荐
- bash shell 编程练习
原始文件: find /etc -name passwd 2>&1 | tee ee.log 1. cat -n 把 e.log 的文档内容加上行号后输入 e2.log 这个文档里: x ...
- 设置 DNS,防止 DNS 污染,清除 DNS 缓存ipconfig /flushdns
设置 DNS,防止 DNS 污染选中“使用下面的 DNS 服务器地址”,“首选 DNS 服务器”中填写 8.8.8.8,“备用 DNS 服务器”中填写 8.8.4.4,然后点击“确定”按钮清除 DNS ...
- H.264流媒体协议格式中的Annex B格式和AVCC格式深度解析
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Romantic_Energy/article/details/50508332本文需要读者对H.26 ...
- 怎样从外网访问内网Nginx?
本地安装了一个Nginx,只能在局域网内访问,怎样从外网也能访问到本地的Nginx呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Nginx 默认安装的Nginx端口是80. 实现步 ...
- Cheerleaders UVA - 11806
题目大意是: 在一个m行n列的矩形网格中放置k个相同的石子,问有多少种方法?每个格子最多放一个石子,所有石子都要用完,并且第一行.最后一行.第一列.最后一列都要有石子. 容斥原理.如果只是n * m放 ...
- ltp-ddt的makefile结构
顶层makefile COMMON_TARGETS := pan utils COMMON_TARGETS += tools testcases/ddt COMMON_TARGET ...
- Docker学习笔记之通过 Dockerfile 创建镜像
0x00 概述 由于 Docker 镜像的结构优势,使它的占用空间远小于普通的虚拟机镜像,而这就大幅减少了 Docker 镜像在网络或者其他介质中转移所花费的时间,进而提高了我们进行迁移部署的效率.不 ...
- css盒子模型、边框border、外边距margin、填充padding、轮廓outline
盒子模型:盒子默认的宽度为容器的宽度,也可以自省设定宽度,高度根据内容适应,也可以自行设定高度.min-height设定最小高度 一个盒子包括外边距.边框.内边距和实际内容 Margin(外边距):清 ...
- 记录Linux中遇到的技巧
压缩排除.svn目录 打包test目录,排除其中所有子目录中的.svn文件夹: # tar -zcf test.tar.gz test/ --exclude=.svn# zip -qr test.zi ...
- mysql 5.7/percona server/mariadb 10.2安装与服务器参数优化
建议使用percona server linux generic版,从https://www.percona.com/downloads/Percona-Server-LATEST/下载,现在不在推荐 ...