UVA-11624 Fire!(两次BFS)

18 篇文章 0 订阅
3 篇文章 0 订阅

Fire!

Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.

Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.

Input Specification

The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:

·  #, a wall

·  ., a passable square

·  J, Joe's initialposition in the maze, which is a passable square

·  F, a square that is onfire

There will be exactly one J in each test case.

Sample Input

2

4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F

Output Specification

For each test case, output a single line containing IMPOSSIBLE if Joe cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.

Output for SampleInput

3

IMPOSSIBLE

分析:1. 转自 2. 转自

题目:一个平面迷宫中有一个人,迷宫中有些点起火了,火和人每个单位时间只能向相邻的格子移动,

            其中有一些空间被墙壁占据,问这个人在不背或烧到的情况下,离开迷宫的最快时间。

分析:搜索。迷宫中的最短路,首先就会想到bfs;并且bfs利用队列会使状态空间按时间顺序分层。

            而火的扩散过程正好符合这个时间的层次。所以我们会想到,利用两个队列,一个储存人的状态,

            一个储存火的状态。按照时间顺序,先更新火蔓延的节点,再扩展人能到达的节点。

            通过分析,我们发现这两个队列可以合并,只须初始化的时候,按照火节点然后是人的顺序入队即可。

           (节点中加入一个是否是火节点的判断,就可以两种节点按不同的细节处理)

注意:时间是指走出迷宫的时间,到达边界后要加1;初始节点的判断。

也就是说,Joe的起点唯一,但是起火的地方并不是唯一的,这是我们首先要明确的。

关键是之后应该怎样处理火势的蔓延与Joe逃跑这两个过程的关系,我们要清楚:只有火势可以影响Joe的逃跑路线,但Joe的逃跑路线绝对不能影响火势,所以这两个过程不可能出现在同一个BFS中。

可以这样想:既然火势的蔓延时不随人的主观意愿而改变的,那么我们可以先让火势肆意蔓延,看它到底能烧到哪里,以及烧到某个地方所需要的时间,这样,主人公在逃跑的过程中,只要在火势到达之前赶到某个地方就可以了。

综上,需要两个BFS,第一个计算火势蔓延到任意一点所需要的时间,如果火势永远到达不了某些点,就把这些点的时间设为正无穷,之后再搜索Joe的逃跑路线,条件要增加时间这一项,只要Joe能到达迷宫的边界,就算逃出来了。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1010;

struct node
{
	int x, y, t, id;///id区分是J还是F
};

int n, m, ans, sum;
bool map[N][N];
char str[N];
queue<node>q;

int f[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

bool judge(int x ,int y)
{
   if(x >= 0 && x < n && y >= 0 && y < m && !map[x][y]) return true;
   return false;
}

int bfs()
{
	node nex;
	while(!q.empty())
	{
	    node a = q.front();
		q.pop();
		for(int i = 0; i < 4; i++)
		{
			nex.x = a.x + f[i][0];
			nex.y = a.y + f[i][1];
			nex.t= a.t + 1;
			nex.id = a.id;
			///当Joe接触到迷宫边缘时便可逃出
			if(nex.id && (nex.x == -1 || nex.x == n || nex.y == -1 || nex.y == m))
					 return nex.t;
			if(judge(nex.x, nex.y))
			{
				map[nex.x][nex.y] = 1;
				q.push(nex);
			}
		}
	}
	return -1;
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		node s1, s2;
		scanf("%d%d", &n, &m);
		while(!q.empty()) q.pop();
		for(int i = 0; i < n; i++)
		{
			scanf("%s", str);
		    for(int j = 0; j < m; j++)
		    {
		    	if(str[j] == '.') map[i][j] = 0;
            	else if(str[j] == '#') map[i][j] = 1;
		    	else if(str[j] == 'F')
		    	{
		    		map[i][j] = 1, s1.x = i, s1.y = j, s1.t = 0, s1.id = 0;
		    		q.push(s1);
		    	}

		    	else if(str[j] == 'J')
					 map[i][j] = 1, s2.x = i, s2.y = j, s2.t = 0, s2.id = 1;
		    }
		}q.push(s2);///Joe最后加入队列
		int ans = bfs();
		ans == -1 ? puts("IMPOSSIBLE") : printf("%d\n", ans);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值