J - Fire! //两路BFS

J - Fire!

题目:

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是火的初始位置


AC代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 1005
const int INF=0xfffffff;
struct node
{
	int x,y;
 } Joe,Fire[maxn*maxn];
 char mp[maxn][maxn];
 int vJoe[maxn][maxn];
 int vFire[maxn][maxn];
 int dir[4][2]={0,1,1,0,0,-1,-1,0};
 int m,n,nFire;
 void bfsFire()
 {
 	  node s,q;
 	  queue<node>Q;
 	  int i;
 	  for(int i=0;i<nFire;i++)
 	  {
 	  	Q.push(Fire[i]);
 	  	vFire[Fire[i].x][Fire[i].y]=1;
	   }
	   while(!Q.empty())
	   {
	   	s=Q.front();
	   	Q.pop();
	   	for(int i=0;i<4;i++)
	   	{
	   	   q=s;
		   q.x+=dir[i][0];
		   q.y+=dir[i][1];
		   if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&mp[q.x][q.y]!='#'&&vFire[q.x][q.y]==INF)	
		   {
		   	vFire[q.x][q.y]=vFire[s.x][s.y]+1;
		   	Q.push(q);
		   }
		 }
	   }
 }
 int check(int x,int y)
 {
 	if((x==0||x==m-1||y==0||y==n-1)&&vJoe[x][y]<vFire[x][y])
 	return 1;
 	return 0;
 } 
 int bfsJoe()
 {
 	queue<node>Q;
 	node s,q;
 	int i;
 	vJoe[Joe.x][Joe.y]=1;
 	Q.push(Joe);
 	while(Q.size())
 	{
 		s=Q.front();
 		Q.pop();
 		if(check(s.x,s.y)==1)
 		{
 			return vJoe[s.x][s.y];
		 }
		for(int i=0;i<4;i++)
		{
			q=s;
			q.x+=dir[i][0];
			q.y+=dir[i][1];
			if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&mp[q.x][q.y]!='#'&&vJoe[q.x][q.y]==0)
			{
				vJoe[q.x][q.y]=vJoe[s.x][s.y]+1;
				Q.push(q);
			}
		}
	 }
	 return -1;
 }
 
 int main()
 {
     int T;
	 scanf("%d",&T);
	 while(T--)
	 {
	   int i,j,ans;
	   scanf("%d%d",&m,&n);
	   nFire=0;
	   for(int i=0;i<m;i++)
	   {
	    	scanf("%s",mp[i]);
	     for(int j=0;j<n;j++)
		 {
		    vJoe[i][j]=0;
			vFire[i][j]=INF;
			if(mp[i][j]=='J')
			{
			   Joe.x=i;
			   Joe.y=j;	
			 } 	
			 if(mp[i][j]=='F')
			 {
			 	Fire[nFire].x=i;
			 	Fire[nFire++].y=j;
			 }
	     }   	
		} 	
		bfsFire();
		ans=bfsJoe();
		if(ans==-1)
		{
			printf("IMPOSSIBLE\n");
		}
		else
		{
			printf("%d\n",ans);
		}
	  } 	
	  return 0;
 } 


猜你喜欢

转载自blog.csdn.net/lmengi000/article/details/80488694