[kuangbin带你飞]专题1 简单搜索 J - Fire! 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 <= 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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值