9.C - 作业题(建议使用BFS)

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<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define int ll
using namespace std;
const int N=1e6+7;
const int M=2e3+7;
int n,m;
bool vis[M][M];
int fire[M][M];
char mp[M][M];
int nx[]={0,0,1,-1};
int ny[]={1,-1,0,0};
struct A
{
	int x,y,step;
}a,b,c,d;
queue<A> q1;
queue<A> q2;
void fire_bfs()
{
	fire[a.x][a.y]=0;
	q1.push(a);
	while(!q1.empty())
	{
		a=q1.front();
		q1.pop();
		for(int i=0;i<=3;i++)
		{
			int xx=a.x+nx[i];
			int yy=a.y+ny[i];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&(mp[xx][yy]=='.'||mp[xx][yy]=='J')&&a.step+1<fire[xx][yy])
			{
				fire[xx][yy]=a.step+1;
				b.x=xx,b.y=yy,b.step=a.step+1;
				q1.push(b);
			}
		}
	}
}
void j_bfs()
{
	vis[c.x][c.y]=1;
	q2.push(c);
	while(!q2.empty())
	{
		c=q2.front();
		q2.pop();
		if(c.x==1||c.x==n||c.y==1||c.y==m)
		{
			cout<<c.step+1<<endl;
			return;
		}
		for(int i=0;i<=3;i++)
		{
			int xx=c.x+nx[i];
			int yy=c.y+ny[i];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]!='#'&&!vis[xx][yy]&&c.step+1<fire[xx][yy])
			{
				vis[xx][yy]=1;
				d.x=xx,d.y=yy,d.step=c.step+1;
				q2.push(d);
			}
		}
	}
	cout<<"IMPOSSIBLE"<<endl;
	return;
}
signed main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
				cin>>mp[i][j];
		memset(vis,0,sizeof vis);
		memset(fire,0x3f,sizeof fire);
		while(!q1.empty()) q1.pop();
		while(!q2.empty()) q2.pop();
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(mp[i][j]=='F')
				{
					a.x=i,a.y=j,a.step=0;
					fire_bfs();
				}
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(mp[i][j]=='J')
				{
					c.x=i,c.y=j,c.step=0;
					j_bfs();
				}
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值