# UVA11624 Fire! （BFS）

DFS BFS 同时被 2 个专栏收录
5 篇文章 0 订阅
20 篇文章 0 订阅

Fire!

Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.

Given Joe’s location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.

Input Specification

The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:

· #, a wall

· ., a passable square

· J, Joe’s initialposition in the maze, which is a passable square

· F, a square that is onfire

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 cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.

Output for SampleInput

3
IMPOSSIBLE

UVA11624 Fire! 两次BFS 读懂题意很重要

（以上内容均转自博主的博客）转自这里

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define judge ne.x >= 0 && ne.x < n && ne.y >= 0 && ne.y < m
#define confirm vis[ne.x][ne.y] = 1
using namespace std;

struct str
{
int x,y,step;
} fir,ne;

using namespace std;

int n,m,a,b;
int fire[1010][1010];
char s[1010][1010];
int nex[4] = {0,0,1,-1};
int ney[4] = {1,-1,0,0};
int vis[1010][1010];

queue<str> q;

void clear_q()
{
while(!q.empty())
q.pop();
}

void bfs_fire()
{
memset(vis,0,sizeof(vis));
while(q.size())
{
fir = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
ne.x = fir.x+nex[i];
ne.y = fir.y+ney[i];
ne.step = fir.step+1;
if(vis[ne.x][ne.y] == 0 && (judge) && s[ne.x][ne.y] != '#')
{
fire[ne.x][ne.y] = ne.step;
confirm;
q.push(ne);
}

}
}
}

void bfs_joe(int u,int v)
{
memset(vis,0,sizeof(vis));
clear_q();
fir.x = u;
fir.y = v;
fir.step = 0;
q.push(fir);
while(q.size())
{
fir = q.front();
if(fir.x == 0 || fir.x == n-1 || fir.y == 0 || fir.y == m-1)
{
printf("%d\n",fir.step+1);
return ;
}
q.pop();
for(int i = 0; i < 4; i++)
{
ne.x = fir.x+nex[i];
ne.y = fir.y+ney[i];
ne.step = fir.step+1;
if((judge) && fir.step+1 < fire[ne.x][ne.y] && vis[ne.x][ne.y] == 0 && s[ne.x][ne.y] == '.')
{
confirm;
q.push(ne);
}
}
}
printf("IMPOSSIBLE\n");
}

int main()
{
int t,a,b;
scanf("%d",&t);
while(t--)
{
memset(fire,0x3f,sizeof(fire));
clear_q();
scanf("%d %d",&n,&m);
getchar();
for(int i = 0; i < n; i++)
{
scanf("%s",s[i]);
for(int j = 0; j < m; j++)
{
if(s[i][j] == 'J')
{
a = i;
b = j;
}
else if(s[i][j] == 'F')
{
fir.x = i;
fir.y = j;
fir.step = 0;
q.push(fir);
}
}
}
bfs_fire();
bfs_joe(a,b);
}
return 0;
}


#include <iostream>
#include <queue>
#include <memory.h>
#include <stdio.h>
#define MAX 1100
using namespace std;
struct P//用来存储路径的节点
{
int r,c;
int step;
P(int _r,int _c,int _s)
{
r=_r,c=_c,step=_s;
}
P(){}
};

const int dr[] = {0,0,1,-1};//构造函数
const int dc[] = {1,-1,0,0};
char Map[MAX][MAX];//存储地图
int visit[MAX][MAX];//BFS中最重要的visit数组
int times[MAX][MAX];//记录每个地方被烧到所需的时间
int r,c;//行，列
int Step;//记录最终逃脱所需的步数或时间
P J;
queue<P> q;//全局队列，这是本题的关键，也是一个大坑

void clear_q()//用来清除队列中的数据
{
while(!q.empty())
q.pop();
}

void show_time()//调试使用
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<times[i][j]<<' ';
}
cout<<endl;
}
}

void bfs_fire()//预处理，求出每一个位置被烧到所需要花的时间
{
int i;
int R,C,Time;
while(!q.empty())
{
P temp=q.front();
q.pop();
//cout<<temp.r<<' '<<temp.c<<endl;
for(i=0;i<4;i++)
{
R=temp.r+dr[i];
C=temp.c+dc[i];
Time=temp.step+1;
if(R>=0&&R<r&&C>=0&&C<c&&(Map[R][C]=='.'||Map[R][C]=='J')&&visit[R][C]==0)
{
//cout<<R<<' '<<C<<endl;
visit[R][C]=1;
q.push(P(R,C,Time));
times[R][C]=Time;
}
}
}
}

int bfs_joe()//逃跑路线，每走一个位置，不仅要看是否符合一般条件，还要用到上一步求出的时间
{
int i;
int R,C,Time;
clear_q();
q.push(J);
visit[J.r][J.c]=1;
while(!q.empty())
{
P temp=q.front();
q.pop();
//cout<<temp.r<<' '<<temp.c<<endl;
if(temp.r==0||temp.r==(r-1)||temp.c==0||temp.c==(c-1))
{
Step=temp.step+1;
return 1;
}
for(i=0;i<4;i++)
{
R=temp.r+dr[i];
C=temp.c+dc[i];
Time=temp.step+1;
if(R>=0&&R<r&&C>=0&&C<c&&Map[R][C]=='.'&&visit[R][C]==0&&Time<times[R][C])
{
visit[R][C]=1;
q.push(P(R,C,Time));
}
}
}
return 0;
}

int main()
{
int i,j;
int t;
cin>>t;
while(t--)
{
clear_q();//清空队列
cin>>r>>c;
for(i=0;i<r;i++)
{
cin>>Map[i];
for(j=0;j<c;j++)
{
if(Map[i][j]=='F')//本题最最坑爹的地方，题意并没有直接对着火点的数量进行说明，但确实说了，隐含在portions 这个单词中，fuck，英文不好真捉急
{
q.push(P(i,j,0));
times[i][j]=0;
visit[i][j]=1;
}
if(Map[i][j]=='J')
{
J.r=i;
J.c=j;
J.step=0;
}
}
}

for(i=0;i<r;i++)//每个点的初始被烧所需的时间应该设为正无穷
{
for(j=0;j<c;j++)
{
times[i][j]=1000000007;
}
}
memset(visit,0,sizeof(visit));//visit数组是两个BFS公用的，所以需要初始化一下
bfs_fire();
//show_time();
memset(visit,0,sizeof(visit));
if(bfs_joe())
cout<<Step<<endl;
else
cout<<"IMPOSSIBLE"<<endl;

}
return 0;
}

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

11-05 324
03-07 100

11-19 124
10-23 718
04-23 60
01-17 234
07-07 199
08-06 354
09-24 69
04-11 96
08-10 146
10-20 383
02-03 24

LSlong127

¥2 ¥4 ¥6 ¥10 ¥20

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