[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;
 } 

 

### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin专题十四涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单搜索题目而言,在为期约两周的时间里完成了这部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值