UVA - 11624 Fire! ——BFS

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


题意:J是人开始的位置 F是火开始烧的地方 人每次只能走一步 火可以向四个方向燃烧 问人能不能活着走出去 能的话需要几步


思路:当时是建了两个队列同时进行宽搜 也可以分开搜 搜一个储存下步数即可

印象中有好几个坑点 可能没有火


#include <iostream>
#include <queue>
#define max 1010
using namespace std;
char map[max][max];
int m,n,jx,jy,count=0,flag=0;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
struct point
{
	int x,y,step;
}z;
queue<point>q1,q2;
point pp[max][max];
void bfs()
{
	q2.push(pp[jx][jy]);
	count=0,flag=0;
	while(q2.size())
	{
		if(flag==1)
		break;
		int k,tx,ty,l;
		if(q1.size())
		{
			l=q1.front().step;
			while(q1.size()&&q1.front().step==l)
	  		{
	  			z=q1.front();
				q1.pop();
				for(k=0;k<=4;k++)
				{
					tx=z.x+dir[k][0];
					ty=z.y+dir[k][1];
					if(tx>=1&&ty>=1&&tx<=n&&ty<=m)
					{
						if(map[tx][ty]!='#'&&map[tx][ty]!='F')
						{
							map[tx][ty]='F';
							pp[tx][ty].step=count+1;
							q1.push(pp[tx][ty]);
						}
					}
				}
			}
		}
		l=q2.front().step;
		while(q2.size()&&l==q2.front().step)
		{
			z=q2.front();
			q2.pop();
			for(k=0;k<4;k++)
			{
				tx=z.x+dir[k][0];
				ty=z.y+dir[k][1];
				if(tx>=1&&ty>=1&&tx<=n&&ty<=m)
				{
					if(map[tx][ty]=='.')
					{
						if(tx==1||ty==1||tx==n||ty==m)
						{
							flag=1;
							break;
						}
						map[tx][ty]='J';
						pp[tx][ty].step=count+1;
						q2.push(pp[tx][ty]);
					}
				}
			}
		}
		count++;
	}
}
int main()
{
	ios::sync_with_stdio (false);
	int t;
	cin>>t;
	while(t--)
	{
		count=0;
		while(q1.size())
			q1.pop();
		while(q2.size())
			q2.pop();
		cin>>n>>m;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>map[i][j];
				pp[i][j].x=i;
				pp[i][j].y=j;
				pp[i][j].step=-1;
				if(map[i][j]=='J')
				{
					jx=i;jy=j;
					pp[i][j].step=0;
				}
				if(map[i][j]=='F')
				{
					pp[i][j].step=0;
					q1.push(pp[i][j]);
				}
				
			}
		}
		if(jx==1||jy==1||jx==n||jy==m)
			flag=1;
		else
			bfs();
		if(flag)
			cout<<count+1<<endl;
		else
			cout<<"IMPOSSIBLE"<<endl;
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值