UVa 11624 Fire!(着火了!)

时间:2022-08-10 16:13:20


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

UVa 11624 Fire!(着火了!)

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 ;
}