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 ≤ 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

思路:先把火push进队列里让其先跑之后再跑开始终点,当跑的输出ac

代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
struct node{
	int x,y,step,tt;
	node(){
	}
	node(int xx,int yy,int sstep,int ttt):x(xx),y(yy),step(sstep),tt(ttt){
	}
};
queue<node>A;int n,m;
char map1[1009][1009];int book[1009][1009],book1[1009][1009];
const int rr[4][2]={1,0,0,1,-1,0,0,-1};
int bfs(int x,int y,int step)
{
	A.push(node(x,y,step,1));
	while(!A.empty())
	{
		node u = A.front();
		A.pop();
		for(int i = 0;i < 4;++i)
		{
			int xx = u.x + rr[i][0];
			int yy = u.y + rr[i][1];
			int ss = u.step + 1;
			if((xx < 1 || xx > n || yy < 1 || yy > m )&& u.tt == 1)	
			{
				return ss;//跑到ac
			}
			if(xx < 1 || xx > n || yy < 1 || yy > m)
			{
				continue;
			}
			if(book[xx][yy] == 1 || map1[xx][yy] == '#')
			{
				continue;
			}
			book[xx][yy] = 1;
			A.push(node(xx,yy,ss,u.tt));
		}
	}
	return -1;
}
int main()
{
	int t;int xstart,ystart;
	scanf("%d",&t);
	while(t--)
	{
		memset(map1,0,sizeof(map1));
		memset(book,0,sizeof(book));
		scanf("%d%d",&n,&m);string a;
		for(int i = 1;i<=n;++i)
		{
			cin>>a;
			for(int j = 1; j <= m;++j)
			{
				map1[i][j] = a[j-1];
				if(map1[i][j] == 'F')
				{
					book[i][j] = 1;
					A.push(node(i,j,0,0));//先跑puah火
				}
				if(map1[i][j] == 'J')
				{
					xstart = i;
					ystart = j;
				}
			}
		}
		book[xstart][ystart] = 1;
		int gg = bfs(xstart,ystart,0);后跑
		if(gg == -1)
		{
			printf("IMPOSSIBLE\n");
		}
		else{
			printf("%d\n",gg);
		}
		while(!A.empty())
		{
			A.pop();
		}
	}
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/qq_43568078/article/details/89161460