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

本文介绍了一个迷宫逃生问题,利用BFS算法模拟人物与火势的移动,计算人物能否及何时安全逃离迷宫。

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

题目:

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 <= RC <= 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

Output for Sample Input

3
IMPOSSIBLE


题意:在一个迷宫中Joe要从初始位置跑出地图(到达边界即成功),在他跑的同时火也在扩散。火烧的方向,Joe跑的方向都可以为上下左右,每秒走一步。他必须在火到达他的当前位置之前跑出去,问他是否可以跑出地图,跑出去的最早时间是多少。

#是墙

.是可行路径

J是Joe的初始位置

F是火的初始位置


思路:BFS两次。第一次求火烧到所有可以到达的位置的最小时间,第二次BFS求Joe在火烧的同时(到达某一位置时火烧到的时间一定要比其大)是否可以到达边界·,以及逃出迷宫的时间。


#include<bits/stdc++.h>

typedef long long ll;

using namespace std;

const int MAXN = 1010;

int n,m;
char G[MAXN][MAXN];
int fire[MAXN][MAXN];  //火烧到(i,j)的最小时间 
int Time[MAXN][MAXN];  //人到达(i,j)的最小时间 
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};

void bfsFire(){   //求火烧到(i,j)的最小时间 
	memset(fire,-1,sizeof(fire));
	queue< pair<int,int> > q;
	for(int i = 0 ; i<n ; i++){
		for(int j = 0 ; j<m ; j++){
			if(G[i][j] == 'F'){
				fire[i][j] = 0;                //F为火的初始位置,时间为0 
				q.push(make_pair(i,j));
			}
		}
	} 
	while(!q.empty()){
		pair<int,int> temp = q.front();
		q.pop();
		int x = temp.first; int y = temp.second;
		
		for(int i = 0 ; i<4 ; i++){
			int nxtx = x+dir[i][0];
			int nxty = y+dir[i][1];
			
			if(nxtx<0 || nxtx>=n || nxty<0 || nxty>=m) //地图之外 
				continue;
			if(fire[nxtx][nxty]!=-1)                   //火之前已经烧到 
				continue; 
			if(G[nxtx][nxty] == '#')                   //是墙 
				continue;
			
			fire[nxtx][nxty] = fire[x][y] + 1;
			q.push(make_pair(nxtx,nxty));
		}
	}
} 

int bfs(){   //求人跑出迷宫的最小时间 
	queue< pair<int,int> > q;
	memset(Time,-1,sizeof(Time));
	
	for(int i = 0 ; i<n ; i++){
		for(int j = 0 ; j<m ; j++){
			if(G[i][j] == 'J'){
				q.push(make_pair(i,j));
				Time[i][j] = 0;              //J为JOE逃跑的起始位置,时间为0 
			}
		}
	}
	
	while (!q.empty()){
		pair<int,int> temp = q.front();
		q.pop();
		
		int x = temp.first;
		int y = temp.second;
		
		if(x==0 || y==0 || x==n-1 || y==m-1)   //能顺利到达边界即为逃出边界。逃出去的时间要+1,即从边界跑出去 
			return Time[x][y]+1;
			
		for(int i = 0 ; i<4 ; i++){
			int nxtx = x+dir[i][0];
			int nxty = y+dir[i][1];
			
			if(Time[nxtx][nxty] != -1)      //之前已经搜索过 
				continue;
			if(nxtx<0 || nxtx>=n || nxty<0 || nxty>=m)   //下一步在规定范围之外 
				continue;
			if(G[nxtx][nxty] == '#')                //下一步是墙 
				continue;
			if(fire[nxtx][nxty]!=-1 && Time[x][y]+1>=fire[nxtx][nxty])  //人最小到达的时间比火烧到的时间大 
				continue;
			
			Time[nxtx][nxty] = Time[x][y] + 1;
			q.push(make_pair(nxtx,nxty));
		} 
	} 
	return -1;
}

int main(){
	ios::sync_with_stdio(false);
	int T;
	scanf("%d",&T);
	while (T--){
		scanf("%d %d",&n,&m); 
		for(int i = 0 ; i<n ; i++){
			scanf("%s",G[i]);
		}
		bfsFire();
		
		int ans = bfs();
		if(ans == -1)
			printf("IMPOSSIBLE\n");
		else
			printf("%d\n",ans);
	}
		
	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、付费专栏及课程。

余额充值