博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 11624 Fire!
阅读量:5809 次
发布时间:2019-06-18

本文共 4024 字,大约阅读时间需要 13 分钟。

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.

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.

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.

Sample Input:

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Sample Output:
3
IMPOSSIBLE

题意:有一个迷宫,那么又是逃离了,现在知道Joe的初始位置,也知道火种的初始位置,Joe和火种是可以同时移动的(上下左右),这次要逃离的标准是Joe比火种先到边界,任何一个边界都可以,就算逃离成功,那么我们只需要分别统计Joe和火种达到每一点需要的步数(时间),然后在边界位置进行比较就可以了,两个BFS就行了(关键是火种可能不止一处有,也可能一处都没有)。

#include
#include
#include
#include
using namespace std;const int N=1100;const int INF=0x3f3f3f3f;struct node{ int x, y;}no[N]; ///no结构体记录每个火种的初始位置char Map[N][N];int k, m, n, X, Y, visJ[N][N], visF[N][N]; ///k表示火种的个数,X和Y记录Joe的初始位置,visJ数组保存Joe到达每一点需要的时间,visF数组保存火种到达每一点需要的时间int dir[4][2] = { {
1,0}, {-1,0}, {
0,1}, {
0,-1} };void Init(){ int i, j; for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { visJ[i][j] = 0; visF[i][j] = INF; ///这里如果和visJ一样初始化为0,那么当没有火种时,visJ显然是不能小于visF的,那不就不能逃离了,其实这种情况下只要边界不是'#',就可以逃离,所以初始化为INF } } k = 0;}void BFSF() ///计算火种到达每一点的时间{ int i, x, y; node next, now; queue
Q; for (i = 0; i < k; i++) { now.x = no[i].x; now.y = no[i].y; visF[now.x][now.y] = 1; Q.push(now); } while (!Q.empty()) { now = Q.front(); Q.pop(); for (i = 0; i < 4; i++) { next.x = x = now.x + dir[i][0]; next.y = y = now.y + dir[i][1]; if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] != '#' && visF[x][y] == INF) { visF[x][y] = visF[now.x][now.y] + 1; Q.push(next); } } }}int BFSJ() ///计算Joe到达每一点的时间,并在边界与火种比较{ int i, x, y; node next, now; queue
Q; now.x = X; now.y = Y; Q.push(now); visJ[X][Y] = 1; while (!Q.empty()) { now = Q.front(); Q.pop(); if ((now.x == 0 || now.x == m-1 || now.y == 0 || now.y == n-1) && visJ[now.x][now.y] < visF[now.x][now.y]) return visJ[now.x][now.y]; for (i = 0; i < 4; i++) { next.x = x = now.x + dir[i][0]; next.y = y = now.y + dir[i][1]; if (x >= 0 && x < m && y >= 0 && y < n && Map[x][y] != '#' && !visJ[x][y]) { visJ[x][y] = visJ[now.x][now.y] + 1; Q.push(next); } } } return -1;}int main (){ int T, i, j, ans; scanf("%d", &T); while (T--) { scanf("%d%d", &m, &n); for (i = 0; i < m; i++) scanf("%s", Map[i]); Init(); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (Map[i][j] == 'J') { X = i; Y = j; } else if (Map[i][j] == 'F') ///火种可能不止一处 { no[k].x = i; no[k++].y = j; } } } BFSF(); ans = BFSJ(); if (ans == -1) printf("IMPOSSIBLE\n"); else printf("%d\n", ans); } return 0;}

转载于:https://www.cnblogs.com/syhandll/p/4840424.html

你可能感兴趣的文章
Java 的垃圾回收机制(转)
查看>>
腾讯电竞:电竞产业已成风口 良性发展需要靠教育稳固根基
查看>>
为什么运营商集体降价还挨骂?
查看>>
再见!春节红包大战隐退是策略转移
查看>>
RMI
查看>>
MIT研究员使用无人机跟踪仓库库存
查看>>
【SSH系列】Hibernate映射 -- 一对一单向关联映射
查看>>
MyBatis Review——动态sql
查看>>
设备与驱动的关系以及设备号、设备文件
查看>>
如何将时间序列问题用 Python 转换成为监督学习问题
查看>>
实践 | Otto奥托集团的架构选型之路
查看>>
portainer,用于管理docker swarm,好像也不错哟
查看>>
加速人工智能落地 阿里云发布全新FPGA计算实例F2
查看>>
大数据分析质量堪忧 盲目决策隐患增加
查看>>
iOS中 最新收集的代码块(汇总) 韩俊强的博客
查看>>
云计算:与国外站在同一起跑线
查看>>
中国人工智能学会通讯——金融机构的市场机遇与实践 1.2 市场机遇——三大转型...
查看>>
从零学React Native之14 网络请求
查看>>
P2P平台众信金融上线半年交易额近10亿
查看>>
完全面向于初学者的Node.js指南
查看>>