# UVA 11624 Fire! bfs

bfs 专栏收录该内容
20 篇文章 0 订阅

UVA 11624

Fire!

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 Specification

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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE

Malcolm Sharpe, Ondřej Lhoták

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#define ll long long
#define pi acos(-1)
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int>P;
int n,m;
char mp[1005][1005];
bool book[1005][1005];
int tp[1005][1005];
int go[4][2]={1,0,0,1,-1,0,0,-1};
struct node{
int r,c;
int time;
}J;
void fbfs()
{
queue<node>q;
while(!q.empty())
q.pop();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(mp[i][j]=='F')
{
tp[i][j]=0;
q.push({i,j,0});
book[i][j]=1;
}
}
}
while(!q.empty())
{
node t=q.front(),x;
q.pop();
for(int i=0;i<4;i++)
{
x.r=t.r+go[i][0];
x.c=t.c+go[i][1];
x.time=t.time+1;
if(x.r<0||x.r>=n||x.c<0||x.c>=m||mp[x.r][x.c]=='#'||book[x.r][x.c])continue;
if(tp[x.r][x.c]!=-1)continue;
book[x.r][x.c]=1;
tp[x.r][x.c]=x.time;
q.push(x);
}
}
}
void bfs()
{
queue<node>q;
while(!q.empty())
q.pop();
J.time=0;
book[J.r][J.c]=1;
q.push(J);
while(!q.empty())
{
node t=q.front(),x;
q.pop();
if(t.r==0||t.r==n-1||t.c==0||t.c==m-1)
{
printf("%d\n",t.time+1);
return ;
}
for(int i=0;i<4;i++)
{
x.r=t.r+go[i][0];
x.c=t.c+go[i][1];
x.time=t.time+1;
if(x.r<0||x.r>=n||x.c<0||x.c>=m||mp[x.r][x.c]=='#'||book[x.r][x.c])continue;
if(tp[x.r][x.c]<=x.time&&tp[x.r][x.c]!=-1)continue;
book[x.r][x.c]=1;
q.push(x);
}
}
printf("IMPOSSIBLE\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",mp[i]);
for(int j=0;j<m;j++)
{
if(mp[i][j]=='J')
{
J.r=i;
J.c=j;
}
}
}
memset(tp,-1,sizeof(tp));
memset(book,0,sizeof(book));
fbfs();
//for(int i=0;i<n;i++)
//for(int j=0;j<m;j++)
//printf("%d%c",tp[i][j]," \n"[j==m-1]);
memset(book,0,sizeof(book));
bfs();
}
return 0;
}

if(0<=x.r&&x.r<n&&0<=x.c&&x.c<m&&!book[x.r][x.c]&&mp[x.r][x.c]!='#'&&x.time<tp[x.r][x.c])

• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

04-30 5295
02-16 694

10-13 182
07-10 84
06-24 694
12-30 672
06-27 542
07-17 1073
07-30 284
08-10 147
06-26 777
02-28 161
07-30 398
10-28 470
07-28 173
02-17 93
02-15 97
08-22 291
06-21 36

WA是一笔财富

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。