Fire!
https://vjudge.net/problem/UVA-11624
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
C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 1000 + 5;
const int INF = 0x3f3f3f3f;
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int timeFire[MAXN][MAXN];
int dir[4][2] = {0,1,1,0,0,-1,-1,0};
int n, m;
struct node{
int x, y, step;
};
queue<node> fire;
bool judge(int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < m;
}
void bfsFire()
{
node now, next;
while(!fire.empty())
{
now = fire.front();
fire.pop();
for(int i = 0; i < 4; i ++)
{
int tx = now.x + dir[i][0];
int ty = now.y + dir[i][1];
if(judge(tx, ty) && !vis[tx][ty] && maze[tx][ty] != '#')
{
vis[tx][ty] = true;
timeFire[tx][ty] = timeFire[now.x][now.y] + 1;
next.x = tx;
next.y = ty;
fire.push(next);
}
}
}
}
void bfs(int x, int y)
{
memset(vis, false, sizeof(vis));
queue<node> q;
node now, next;
now.x = x;
now.y = y;
now.step = 0;
vis[x][y] = true;
q.push(now);
while(!q.empty())
{
now = q.front();
q.pop();
for(int i = 0; i < 4; i ++)
{
int tx = now.x + dir[i][0];
int ty = now.y + dir[i][1];
if(tx < 0 || tx >= n || ty < 0 || ty >= m)
{
printf("%d\n", now.step + 1);
return;
}
if(!vis[tx][ty] && maze[tx][ty] != '#' && now.step + 1 < timeFire[tx][ty])
{
vis[tx][ty] = true;
next.x = tx;
next.y = ty;
next.step = now.step + 1;
q.push(next);
}
}
}
printf("IMPOSSIBLE\n");
return;
}
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
int sx, sy; node tmp;
memset(vis, false, sizeof(vis));
memset(timeFire, INF, sizeof(timeFire));
scanf("%d%d", &n, &m);
getchar();
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
scanf("%c", &maze[i][j]);
if(maze[i][j] == 'J'){
sx = i; sy = j;
}
else if(maze[i][j] == 'F'){
tmp.x = i;
tmp.y = j;
fire.push(tmp);
vis[i][j] = true;
timeFire[i][j] = 0;
}
}
getchar();
}
bfsFire();
bfs(sx, sy);
}
return 0;
}
在这款迷宫逃脱游戏中,玩家必须帮助Joe在火势蔓延前找到出口。Joe和火势每分钟移动一格,游戏的目标是在火势到达之前找到迷宫的出口。本文详细介绍了游戏规则和算法实现。
493

被折叠的 条评论
为什么被折叠?



