11624 - Fire! 图论之双BFS

本文介绍了一种使用广度优先搜索(BFS)解决迷宫中人物躲避火焰并寻找最短逃生路径的问题。通过同时模拟人物与火焰的移动,该算法确保人物能够安全地到达迷宫边界。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.
Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst 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 re
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
re 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


点击打开链接



//分析  看到这题目以为很好搞  好久没写 不熟练 搞了一个晚上

首先用BFS人走  book记录有没走过  题目说火不能过墙  所以要用Book记录而不能用map=# 坑  其次总体思路就是扩散  但不同于一般就是要搞一个循环将每一次进队的一个时间点所有都枚举  在代码中num循环那里  之前只记得将火每次扩散 忘了将人走的也要扩散 导致wa  意思是  

   c

  c b c

c b a b c

   c b c

    c 

  一个字母类型代表1分钟   每一个时刻要将四个方向以前进队的拿出来扩散新的进队 而不是像简单的BFS每次出一点枚举  这里是要每次4个点

第一次源点1个  第二次源点周围4个  abcd  第三次就要枚举abcd各自的四个方向  

//AC代码

#include <iostream>
#include <cstdio>
#include <queue>
#include <string.h>
using namespace std;
const int N=1000+10;
char map[N][N];
struct Node{
	int x,y,step;
};
bool book[N][N];
queue<Node> q,fire;
int next[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int n,m;
/*inline void print(){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++)
		printf("%c ",map[i][j]);
		printf("\n");
	}
}*/
int main(){
	bool ok;
	Node node,tmp;
	int t,i,j,fx,fy;
	char c[N];
	scanf("%d",&t);
	while(t--){
		ok=0;
		memset(book,0,sizeof(book));
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++){
			scanf("%s",c);
			for(j=0;j<m;j++){
				map[i][j]=c[j];
				if(c[j]=='F'){
					node.x=i;
					node.y=j;
					fire.push(node);					
			}				
				else if(c[j]=='J'){
			//用book设置人是不是走过了 不要用#代替 不然火不能烧人 
				  book[i][j]=1;					
					node.x=i;
					node.y=j;
					node.step=0;
					q.push(node);
				}
			}			  
	}
		while(!q.empty()){
			int num=q.size();
			for(j=0;j<num;j++){
				node=q.front();
			q.pop();			
			if(map[node.x][node.y]=='F') //这个点不行了 被火烧了 
			   continue;
			for(i=0;i<4;i++){
				int a=node.x+next[i][0];
				int b=node.y+next[i][1];
				if(a<0||a>=n||b<0||b>=m)
				{
					ok=1;
					break;
				}
				//墙和火不能走  忘了+火 超时了 
	     	//用book设置人是不是走过了 不要用#代替 不然火不能烧人 
			 if(map[a][b]=='#'||map[a][b]=='F'||book[a][b]==1) 
			      continue;
			    book[a][b]=1;    
			     tmp.x=a;
			     tmp.y=b;
			     tmp.step=node.step+1;
			     q.push(tmp);
			}
			}
			if(ok)
			break;
			num=fire.size();
			for(j=0;j<num;j++){
				node=fire.front();
				fire.pop();
					for(i=0;i<4;i++){
						int a=node.x+next[i][0];
			         	int b=node.y+next[i][1];
			         	//题目要求障碍物 墙 不能被烧 郁闷 害我wa 
			         	if(a<0||a>=n||b<0||b>=m||map[a][b]=='#'||map[a][b]=='F') 
			         	continue;
			         	map[a][b]='F';
			            tmp.x=a;
			            tmp.y=b;
			            fire.push(tmp);
					}
			}
		//	print();
		}
		if(ok)
		  printf("%d\n",node.step+1);
		else
		  printf("IMPOSSIBLE\n");
		  while(!q.empty()) q.pop();
		  while(!fire.empty()) fire.pop();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值