[kuangbin带你飞]专题一 简单搜索 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 ≤ 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 <iostream>
#include <cstring>
#include <queue>
using namespace std;

struct node
{
	int x,y,t;
	int f;//f为1则为人,为0则为火 
};

int n,m,flag;
char map[1010][1010];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
queue<node>q;

void bfs()
{
	int sum=1;//队列里人的数量 
	node u;
	for(int i=0;i<n;i++)
	    for(int j=0;j<m;j++)
	        if(map[i][j]=='J')
		    	{
		    		node o;
		    		o.x=i;
		    		o.y=j;
		    		o.t=0;
		    		o.f=1;
		    		q.push(o);
				}
	while(!q.empty())
	{
		if(sum==0) return;
		u=q.front();
		q.pop();
		if(u.f==0)
		{
			for(int i=0;i<4;i++)
			{
				int xx=dir[i][0]+u.x;
				int yy=dir[i][1]+u.y;
				if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]=='.')
				{
					map[xx][yy]='F';
					node v;
					v.x=xx,v.y=yy,v.f=0;
					q.push(v);
				}
			}
		}
		else
		{
			sum--;
			if(u.x==0||u.x==n-1||u.y==0||u.y==m-1) //如果已经走到边界,输出结果,结束
            {
                flag=1;
                cout<<u.t+1<<endl;
                return;
            }
            for(int i=0;i<4;i++)
			{
				int xx=dir[i][0]+u.x;
				int yy=dir[i][1]+u.y;
				if(xx>=0&&xx<n&&yy>=0&&yy<m&&map[xx][yy]=='.')
				{
					map[xx][yy]='#';
					node v;
					v.x=xx,v.y=yy,v.t=u.t+1,v.f=1;
					sum++; 
					q.push(v);
				}
			}
		}
	}
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m;
		while(!q.empty())q.pop();
		for(int i=0;i<n;i++)
		    for(int j=0;j<m;j++)
		    {
		    	cin>>map[i][j];
				if(map[i][j]=='F')
		    	{
		    		node o;
		    		o.x=i;
		    		o.y=j;
		    		o.t=0;
		    		o.f=0;
		    		q.push(o);

				}	
			}
		flag=0;//判断是否逃出 
		bfs();
		if(flag==0)cout<<"IMPOSSIBLE"<<endl;
	}
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/weixin_40829921/article/details/81260219