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

解题报告:这道题并不难,坑的是起火点有好几个。。

#include<iostream>
#include<cstring>
#include<stdio.h> 
#include<queue>
using namespace std;
const	int N=1010;
int n,m;
char map[N][N];
bool st[N][N];
struct node{
   
	int x;
	int y;
	int step;
	bool flag;
}FF[N];
int cnt;
int dx[4]={
   0,1,0,-1},dy[4]={
   1,0,-1,0};
void bfs(int x,int y)
{
   
	memset(st,0,sizeof st);
	queue<node>q;
	for(int i=0;i<cnt;i++)
	{
   
		q.push({
   FF[i].x,FF[i].y,FF[i].step,FF[i].flag});
		st[FF[i].x][FF[i].y]=true;
	}
	q.push({
   x,y,0,0});
	st[x][y]=true;
	while(q.size())
	{
   
		node t=q.front();
		q.pop();
	// printf("x=%d,y=%d,flag=%d,time=%d\n",t.x,t.y,t.flag,t.step);
		if(t.flag==0&&(t.x<0||t.y<0||t.x>=n||t.y>=m))
		{
   
			cout<<t.step<<endl;
			return ;
		}
		for(int i=0;i<4;i++)
		{
   
			int tx=t.x+dx[i];
			int ty=t.y+dy[i];
			if(t.flag==1)
			{
   
				if(tx<0||ty<0||tx>=n||ty>=m)	continue;
				if(!st[tx][ty]&&map[tx][ty]!='#')
				{
   
					st[tx][ty]=true;
					q.push({
   tx,ty,t.step+1,t.flag});
				}	
			}
			else 
			{
   
				if(tx<0||ty<0||tx>=n||ty>=m)
				{
   
					q.push({
   tx,ty,t.step+1,t.flag});
				}
				else  if(!st[tx][ty]&&map[tx][ty]=='.')
				{
   
				st[tx][ty]=true;
				q.push({
   tx,ty,t.step+1,t.flag});}
			}
		}
	}
	cout<<"IMPOSSIBLE"<<endl;
}
int main()
{
   
	int t;
	cin>>t;
	while(t--)
	{
   
		cnt=0;
		int sx,sy,sx2,sy2;
		cin>>n>>m;
		for(int i=0;i<n;i++)
		scanf("%s",map[i]);
		for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
   
			if(map[i][j]=='J')
			{
   
				sx=i;
				sy=j;
			}
			if(map[i][j]=='F')
			{
   
				FF[cnt++]={
   i,j,0,1};
			}
		}
		bfs(sx,sy);
	}
	return 0;
}
全部评论

相关推荐

大飞的诡术妖姬:之前看b站多明海有个说法,日本就业竞争非常低的原因不光是毕业学生少,还有很多人干两年不喜欢职场氛围就辞职躺平,位置也空了很多,论吃苦耐劳还得看咱们
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务