H - 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

题意:
有一个农场起火了,Joe和火每次都可以向四周移动一格,问是否能帮助Joe逃出去,能逃出去,输出逃出去的最小时间,不能,输出IMPOSSIBLE。

思路:
火是有多堆的(T-T),被坑了,利用bfs先加入Joe位置,再循环依次加入火堆位置,然后正常搜索,每次mn矩阵中的变化为Joe能走的位置变为J,火蔓延到的位置为F,能逃出去的条件为Joe位置已经超出了题中mn矩阵。

AC代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<math.h>
#include<string.h>
#include<algorithm> 
using namespace std;
int k,m,n;
char map[1005][1005];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
struct P
{
	int x,y,t;
};
bool check(int a,int b)
{
	if(a>=0&&b>=0&&a<m&&b<n&&map[a][b]!='#')
	return true;
	return false;
}
void bfs(P people)
{
	queue<P> q;
	P now,next,fire;
	q.push(people);
	for(int i=0;i<m;i++)//依次向队列中加入火堆位置
	{
		for(int j=0;j<n;j++)
		{
			if(map[i][j]=='F')
			{
				fire.x=i;
				fire.y=j;
				fire.t=0;
				q.push(fire);
			}
		}
	}
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.x<0||now.x>m-1||now.y<0||now.y>n-1)//Joe逃出条件
		{
			cout<<now.t<<endl;
			return;
		}
		for(int i=0;i<4;i++)
		{
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			if(map[now.x][now.y]=='J'&&(next.x<0||next.x>m-1||next.y<0||next.y>n-1))//Joe下一步就逃出去啦
			{
				next.t=now.t+1;
				q.push(next);
			}
			else if(check(next.x,next.y))
			{
				if(map[now.x][now.y]=='J'&&map[next.x][next.y]=='.')//Joe变化
				{
					map[next.x][next.y]='J';
					next.t=now.t+1;
					q.push(next);
				}
				else if(map[now.x][now.y]=='F'&&map[next.x][next.y]!='F')//火堆变化
				{
					map[next.x][next.y]='F';
					next.t=now.t+1;
					q.push(next);
				}
			}
		}
	}
	cout<<"IMPOSSIBLE"<<endl;
}
int main()
{
	cin>>k;
	while(k--)
	{
		memset(map,0,sizeof(map));
		P people,fire;
		scanf("%d %d",&m,&n);
		for(int i=0;i<m;i++)
		{
			scanf("%s",map[i]);
			for(int j=0;j<n;j++)
			{
				if(map[i][j]=='J')
				{
					people.x=i;
					people.y=j;
					people.t=0;
				}
			}
		}
		bfs(people);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、品质保证:项目代码均经过严格测试,确保功能稳定且运行ok。您可以放心下载并立即投入使用,若遇到任何问题,随时欢迎您的反馈与交流。 2、适用广泛:无论您是计算机相关专业(如计算机科学、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业老师,还是企业员工,都适用。 3、多用途价值:该项目不仅具有很高的学习借鉴价值,对于初学者来说,是入门进阶的绝佳选择;当然也可以直接用于 毕业设计、课程设计、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,那该项目代码更是您发挥创意、实现新功能的起点。可以基于此代码进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎交流学习,欢迎借鉴引用,共同探索编程的无穷魅力! 基于人工智能深度学习技术实现肿瘤区域识别诊断系统python源码带后端+项目说明.zip 环境 - Python : **PyTorch 1.10.0 , OpenCV , Flask , TensorRT 8.5.1.7** - Vue , Vue CLI - Node : **axios , ElementUI , ECharts** - Chrome(内核版本60以上) ## 训练 训练的数据来源于国外的数据集。因数据和精力有限只训练了针对直肠肿瘤模型。首先对CT文件进行整理,使用SimpleITK读取CT文件,读取肿瘤的掩膜文件并映射到肿瘤CT图像来获取肿瘤区域,然后进行数据的归一化,预处理后制作训练和测试的数据集。 使用**PyTorch框架**编写。使用**交叉熵损失函数**,**Adam优化器**。 网络结构采用**U-Net**,**U-Net**是基于FCN的一种语义分割网络,适用于做医学图像的分割。结构如下,实际使用稍有改动。 后端 整个系统采取前后分离的方案,确保足够轻量,低耦合。后端采用Python的Flask库,能与AI框架更好的结合,使得系统能更高内聚。 后端运行流程如下 目录管理: | 目录 | 功能 | | ---- | ---- | | uploads | 直接上传目录 | | tmp/ct | dcm文件副本目录 | | tmp/image| dcm读取转换为png目录| | tmp/mask | 预测结果肿瘤掩膜目录| | tmp/draw | 勾画肿瘤后处理结果目录| 系统以图像分割为核心,利用人工智能完成肿瘤区域的识别勾画并提供肿瘤区域的特征来辅助医生进行诊断。有完整的**模型构建、后端架设和前端访问**功能。 医生只需通过web上传ct图像文件,后台就会使用训练好的模型进行肿瘤区域的分割,然后将勾画好肿瘤区域的图像返回,还有肿瘤区域的一些特征(如面积、周长、强度等),并且提供前几次诊断的特征数据并绘制成图表进行对比来辅助医生诊断。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值