[kuangbin带你飞]专题一 简单搜索 J - Fire!

本文介绍了一个迷宫逃生问题的解决方案,使用广度优先搜索算法帮助角色Joe在迷宫中避开火灾并找到安全出口。详细解释了算法的具体实现过程,包括如何处理迷宫地图输入、火灾蔓延及角色移动等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

刚开始人位置和火入队列时,一定要先入火(血的教训)!!!!!

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

struct node
{
	int x,y,t;
	int f;//f为1则为人,为0则为火 
};

int n,m,flag;
char map[1010][1010];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
queue<node>q;

void bfs()
{
	int sum=1;//队列里人的数量 
	node u;
	for(int i=0;i<n;i++)
	    for(int j=0;j<m;j++)
	        if(map[i][j]=='J')
		    	{
		    		node o;
		    		o.x=i;
		    		o.y=j;
		    		o.t=0;
		    		o.f=1;
		    		q.push(o);
				}
	while(!q.empty())
	{
		if(sum==0) return;
		u=q.front();
		q.pop();
		if(u.f==0)
		{
			for(int i=0;i<4;i++)
			{
				int xx=dir[i][0]+u.x;
				int yy=dir[i][1]+u.y;
				if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]=='.')
				{
					map[xx][yy]='F';
					node v;
					v.x=xx,v.y=yy,v.f=0;
					q.push(v);
				}
			}
		}
		else
		{
			sum--;
			if(u.x==0||u.x==n-1||u.y==0||u.y==m-1) //如果已经走到边界,输出结果,结束
            {
                flag=1;
                cout<<u.t+1<<endl;
                return;
            }
            for(int i=0;i<4;i++)
			{
				int xx=dir[i][0]+u.x;
				int yy=dir[i][1]+u.y;
				if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]=='.')
				{
					map[xx][yy]='#';
					node v;
					v.x=xx,v.y=yy,v.t=u.t+1,v.f=1;
					sum++; 
					q.push(v);
				}
			}
		}
	}
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		while(!q.empty())q.pop();
		for(int i=0;i<n;i++)
		    for(int j=0;j<m;j++)
		    {
		    	cin>>map[i][j];
				if(map[i][j]=='F')
		    	{
		    		node o;
		    		o.x=i;
		    		o.y=j;
		    		o.t=0;
		    		o.f=0;
		    		q.push(o);

				}	
			}
		flag=0;//判断是否逃出 
		bfs();
		if(flag==0)cout<<"IMPOSSIBLE"<<endl;
	}
	return 0;
 } 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值