UVA - 11624 Fire! (嵌套BFS)

        Joe works in a maze. Unfortunately, portions of the maze havecaught on fire, and the owner of the maze neglected to create a fireescape plan. Help Joe escape the maze.Given Joe’s location in the maze and which squares of the mazeare on fire, you must determine whether Joe can exit the maze beforethe fire reaches him, and how fast he can do it.Joe and the fire each move one square per minute, vertically orhorizontally (not diagonally). The fire spreads all four directionsfrom each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the firemay enter a square that is occupied by a wall.

Input

    The first line of input contains a single integer, the number of testcases to follow. The first line of each test case contains the twointegers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. Thefollowing R lines of the test case each contain one row of the maze. Each of these lines contains exactlyC 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 thefire 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


一、原题地址

传送门

二、大致题意

    给出一副地图,询问名为J的人能否在火场中逃离出来,每秒人和火都只能行进一格(上下左右四个方向)。


三、思路

    开一个Fire队列供火的更新使用。在BFS人的移动时,当更新完了当前秒内所有人的状态后,我们进行火的位置的更新,火的位置更新也是一个BFS。


四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int 
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }


int T;
int n, m;
int sx, sy;
char mmp[1005][1005];
bool pvis[1005][1005], fvis[1005][1005];
int x[] = { 1,-1,0,0 };
int y[] = { 0,0,1,-1 };
struct Node
{
	int x, y, step;
};
queue<Node>fire;
void read_map()
{
	scanf("%d%d", &n, &m);
	getchar();
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			scanf("%c", &mmp[i][j]);
			if (mmp[i][j] == 'J')
			{
				sx = i, sy = j;
			}
			else if (mmp[i][j] == 'F')
			{
				Node f;
				f.x = i, f.y = j, f.step = 0;
				fvis[i][j] = true;
				fire.push(f);
			}
		}
		getchar();
	}
}
void update_fire()
{
	int now = fire.front().step;
	while (fire.front().step == now)
	{
		Node tt = fire.front();
		fire.pop();
		for (int i = 0; i < 4; i++)
		{
			int xx = tt.x + x[i], yy = tt.y + y[i];
			if (xx >= 1 && xx <= n&&yy >= 1 && yy <= m&&mmp[xx][yy] != '#')
			{
				if (!fvis[xx][yy])
				{
					Node next;
					next.x = xx, next.y = yy, next.step = tt.step + 1;
					fvis[xx][yy] = true;
					mmp[xx][yy] = 'F';
					fire.push(next);
				}
			}
		}
	}
}
void BFS(int sx, int sy)
{
	queue<Node>peo;
	Node t;
	t.x = sx, t.y = sy, t.step = 0;
	peo.push(t);
	pvis[sx][sy] = true;
	while (!peo.empty())
	{
		t = peo.front();
		peo.pop();
		if (!fire.empty())
		{
			if (t.step == fire.front().step)
				update_fire();
		}
		for (int i = 0; i < 4; i++)
		{
			int xx = t.x + x[i], yy = t.y + y[i];
			if (xx >= 1 && xx <= n&&yy >= 1 && yy <= m&&mmp[xx][yy] == '.')
			{
				if (!pvis[xx][yy])
				{
					Node next;
					next.x = xx, next.y = yy, next.step = t.step + 1;
					pvis[xx][yy] = true;
					peo.push(next);
				}
			}
			if (xx<1 || xx>n || yy<1 || yy>m)
			{
				printf("%d\n", t.step + 1);
				return;
			}
		}
	}
	printf("IMPOSSIBLE\n");
	return;
}
int main()
{
	scanf("%d", &T);
	while (T--)
	{
		memset(pvis, false, sizeof(pvis));
		memset(fvis, false, sizeof(fvis));
		while (!fire.empty())fire.pop();
		read_map();
		BFS(sx, sy);
	}
	getchar();
	getchar();
}

猜你喜欢

转载自blog.csdn.net/amovement/article/details/80555830