UVA-11624 Fire!(宽搜)

UVA-11624 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

题意:
多组输入,每组是一个n*m的迷宫,’.‘表示可以走,’#'表示不能走,'J’表示你当前的位置,'F’表示着火的位置,你每秒钟可以上下左右移动一格,着火点则每秒钟向上下左右格子蔓延,你的目标是在最短时间内走到迷宫边缘并出去,若不能出去则输出"IMPOSSIBLE",能走出去就输出所需最短时间。
注意:着火点可能有多处!!!
题解:BFS

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
char map[1005][1005];
bool vis1[1005][1005];//人走过的位置
bool vis2[1005][1005];//火蔓延的位置
struct cc{
	int x,y,step;
};
queue<cc>q1;//人
queue<cc>q2;//着火点 
bool flag=0;//能否逃生 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ans;//最短时间 
void bfs()
{
	while(!q1.empty())
	{
		int tot2=q2.size();
		for(int i=1;i<=tot2;i++)
		{
			cc u=q2.front(); q2.pop();
			for(int j=0;j<4;j++)
			{
				int mx=u.x+dx[j],my=u.y+dy[j];
				if(mx>=1&&mx<=n&&my>=1&&my<=m&&!vis2[mx][my]&&map[mx][my]=='.')
				{
					q2.push((cc){mx,my,0});
					vis2[mx][my]=1;
				}
			}
		}
		int tot1=q1.size();
		for(int i=1;i<=tot1;i++)
		{
			cc u=q1.front(); q1.pop();
			if(u.x==1||u.x==n||u.y==1||u.y==m)//到达迷宫边缘 
			{
				flag=1;
				ans=u.step;
				return;
			}
			for(int j=0;j<4;j++)
			{
				int mx=u.x+dx[j],my=u.y+dy[j];
				if(mx>=1&&mx<=n&&my>=1&&my<=m&&!vis2[mx][my]&&map[mx][my]=='.'&&!vis1[mx][my])
				{
					q1.push((cc){mx,my,u.step+1});
					vis1[mx][my]=1;
				}
			}
		}
		
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T)
	{
		T--;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='J')
				{	
				    map[i][j]='.';
				    q1.push((cc){i,j});
				    vis1[i][j]=1;
				}
				if(map[i][j]=='F')
				{
					map[i][j]=='.';
					q2.push((cc){i,j});
					vis2[i][j]=1;
				}
			}
		}
		bfs();
		if(!flag)
		{
			printf("IMPOSSIBLE\n");
		}
		else
		{
			printf("%d\n",ans+1);
		}
		memset(vis1,0,sizeof(vis1));
		memset(vis2,0,sizeof(vis2));
		while(!q1.empty()) q1.pop();
		while(!q2.empty()) q2.pop();
		flag=0;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值