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;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:28
25届每天都在焦虑找工作的事情0offer情绪一直很低落硬撑着面了一个岗位岗位有应酬的成分面试的时候hr给我出各种场景题问的问题比较犀利&nbsp;有点压力面的感觉感觉有点回答不上来本来就压抑的情绪瞬间爆发了呢一瞬间特别想哭觉得自己特别没用没绷住掉眼泪了事后想想觉得自己挺有病的&nbsp;真的破大防了
喜欢唱跳rap小刺猬...:我觉得没关系吧,之前有一次面试leader给我压力面,我顶住了压力,结果入职的时候发现组里氛围很差,果断跑路。其实从面试就能大概看出组的情况,面试体验好的组倒是不一定好,但是面试体验不好的组。。。就很难说
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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