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. 大数据权限管理工具 Apache Ranger 初识

    资料参考: Apache Ranger – Introduction http://ranger.apache.org/ 阿里云 Ranger简介 Apache Ranger初识 - 阿里云 大数据权 ...

  2. <转>jmeter(五)JDBC Request

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  3. 关于mapreducer 读取hbase数据 存入mysql的实现过程

    mapreducer编程模型是一种八股文的代码逻辑,就以用户行为分析求流存率的作为例子 1.map端来说:必须继承hadoop规定好的mapper类:在读取hbase数据时,已经有现成的接口 Tabl ...

  4. Java常考面试题(一)

    https://blog.csdn.net/linzhiqiang0316/article/details/80473906

  5. 今日总结(linux和plsql)

    #case ...when语句(根据字段不同值显示不同结果) ##1)case ...when语句的使用方法一: 语法格式: case column_name when value1 then res ...

  6. centos 7上nginx+uwsgi 性能调优

    上一章将nginx +uwsgi搭建起来,将keystone挂载后面.但是发现一个问题,如果http请求达到一定量后,nginx直接返回502.这让需要部署大规模openstack集群的我很是头疼,比 ...

  7. js 解密 16进制转10进制,再取ascii码的对应值

    如:\x64 对应 16进制 0x64 转10进制就是 0x64.toString(10) == 100, 查对应的ascii码表得到 ‘d' <div id=code style='displ ...

  8. RHEL6/7 x86_64下cachefilesd占用cpu达到100%

    昨天,有个测试环境cachedfilesd CPU 100%,一直在跑了挺久,经查 1. CacheFiles介绍NFS是一种经常使用到的网络共享文件系统,在分布式环境下,多台服务器的文件共享是一个问 ...

  9. PHP获取Linux当前目录下文件并实现下载功能

    使用nginx转发过去给php server{ listen 9099; server_name 18.5.6.2; location / { proxy_http_version 1.1; root ...

  10. 搭建ldap服务器及web管理服务--phpldapadmin

    系统版本:centos6 安装配置openldap: yum install openldap openldap-servers openldap-clients openldap-devel com ...