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

        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到达迷宫边界就已经算成功,求最短步数

思路:迷宫!!bfs!!迷宫的不可通行的地方是在实时更新的,麻烦的点就在这。

我看了参考了一下网上的,创建一个二维数组来存火到达某位置的最短时间是多少,然后人再bfs的时候进行判断。看代码

#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
int n,m,a,b;
char Map[1010][1010];//地图 
bool vis[1010][1010];//人是否走过 
int dx[] = {-1,1,0,0},dy[] = {0,0,-1,1};//上下左右 
int df[1010][1010];//记录火到达每个可到达的地方所需要的时间 初始值为-1 
struct que
{
	int x,y,t;
	que(int xx,int yy,int tt):x(xx),y(yy),t(tt){}
};
bool check(int x,int y)
{
	if(x<0||x>=n||y<0||y>=m) return false;//越界 
	if(Map[x][y]=='F'||Map[x][y]=='#') return false;//火或者墙 
	
	return true;
}
void bfs_f()
{
	queue<que> q;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(Map[i][j]=='F')//先把火的位置压入队列 
			{
				df[i][j] = 0;//到原地记为0 
				q.push(que(i,j,0));
			}
		}
	}
	while(!q.empty())
	{
		que u = q.front();
		q.pop();
		
		for(int i=0;i<4;i++)
		{
			int xx = u.x+dx[i];
			int yy = u.y+dy[i];
			
			if(!check(xx,yy)||df[xx][yy]!=-1) continue;//非法 
			
			df[xx][yy] = u.t+1;
			q.push(que(xx,yy,df[xx][yy]));
		}
	}
}
void bfs()
{
	queue<que> q;
	q.push(que(a,b,0));
	vis[a][b] = true;
	
	while(!q.empty())
	{
		que u = q.front();
		q.pop();
		
		if(u.x==0||u.x==n-1||u.y==0||u.y==m-1)//到达边界 
		{
			printf("%d\n",u.t+1);
			return;
		}
		for(int i=0;i<4;i++)
		{
			int xx = u.x+dx[i];
			int yy = u.y+dy[i];
			
			if(!check(xx,yy)||vis[xx][yy]) continue;//非法 
			if(u.t+1>=df[xx][yy]&&df[xx][yy]!=-1) continue;//df[xx][yy]==-1是火不能到达的地方,就是这个地方卡了很久很久,具体原因看文章最下面 
		
			vis[xx][yy] = true;
			q.push(que(xx,yy,u.t+1));
		}
	}
	printf("IMPOSSIBLE\n");
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		memset(vis,false,sizeof(vis));
		memset(df,-1,sizeof(df));//初始化 
		
		for(int i=0;i<n;i++)
		{
			scanf("%s",Map[i]);
			for(int j=0;j<m;j++)
			{
				if(Map[i][j]=='J')//记录起点 
				{
					a = i;
					b = j;
				}
			}
		}
		bfs_f();//先对火bfs,把火到达每个能到达的地方的最短时间求出	
		bfs();//人bfs 
	}
	return 0;
}

刚开始有一点没有考虑到,就是火不能到达的地方,但是人能到达的地方,像这样的存火到达的最短时间会是初始值-1,就像这样的火被墙围住的情况,那火到达所有点的时间都会是初始值-1,所以上面那段代码需要加上df[xx][yy]!=-1。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值