BFS(双向) HDOJ 3085 Nightmare Ⅱ

本文介绍了一种双人逃亡与追击问题的算法实现,通过使用两个队列来跟踪两人移动路径,并利用曼哈顿距离评估鬼魂威胁范围。采用BFS遍历方式寻找两人相遇的最短时间。

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

 

题目传送门

题意:一个人去救女朋友,两个人都在运动,还有鬼在"扩散",问最少几秒救到女朋友

分析:开两个队列来表示两个人走过的路,一个人走到的地方另一个人已经vis了,那么就是相遇了,鬼就用曼哈顿距离判断.

 

#include <bits/stdc++.h>
using namespace std;

const int N = 8e2 + 5;
char maze[N][N];
int n, m;
bool vis[N][N][2];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct Point	{
	int x, y;
	Point()	{}
	Point(int x, int y)	: x (x), y (y) {}
};
queue<Point> que[2];
Point mm, gg, gho[2];

int get_dis(int x, int y, int ex, int ey)	{
	return abs (ex - x) + abs (ey - y);
}

bool check2(int x, int y, int tim)	{
	for (int i=0; i<2; ++i)	{
		if (get_dis (x, y, gho[i].x, gho[i].y) <= 2 * tim)	return false;
	}
	return true;
}

bool check(int x, int y, int typ)	{
	if (x < 1 || x > n || y < 1 || y > m || maze[x][y] == 'X')	return false;
	else	return true;
}

void init(void)	{
	while (!que[0].empty ())	que[0].pop ();
	while (!que[1].empty ())	que[1].pop ();
	memset (vis, false, sizeof (vis));
}

bool BFS(int typ, int tim)	{
	int sz = que[typ].size ();
	while (sz--)	{
		Point u = que[typ].front ();	que[typ].pop ();
		if (!check (u.x, u.y, typ) || !check2 (u.x, u.y, tim))	continue;
		for (int i=0; i<4; ++i)	{
			int tx = u.x + dx[i], ty = u.y + dy[i];
			if (!check (tx, ty, typ) || !check2 (tx, ty, tim))	continue;
			if (vis[tx][ty][typ^1])	return true;
			if (vis[tx][ty][typ])	continue;
			vis[tx][ty][typ] = true;
			que[typ].push (Point (tx, ty));
		}
	}
	return false;
}

int run(void)	{
	init ();	
	vis[mm.x][mm.y][0] = true;
	vis[gg.x][gg.y][1] = true;
	que[0].push (Point (mm.x, mm.y));
	que[1].push (Point (gg.x, gg.y));
	int step = 0;
	while (!que[0].empty () || !que[1].empty ())	{
		++step;
		for (int i=0; i<3; ++i)	{
			if (BFS (0, step))	return step;
		}
		if (BFS (1, step))	return step;
	}
	return -1;
}

int main(void)	{
	int T;	scanf ("%d", &T);
	while (T--)	{
		scanf ("%d%d", &n, &m);
		for (int i=1; i<=n; ++i)	{
			scanf ("%s", maze[i] + 1);
		}
		int t = 0;
		for (int i=1; i<=n; ++i)	{
			for (int j=1; j<=m; ++j)	{
				if (maze[i][j] == 'M')	{
					mm.x = i;	mm.y = j;
				}
				else if (maze[i][j] == 'G')	{
					gg.x = i;	gg.y = j;
				}
				else if (maze[i][j] == 'Z')	{
					gho[t].x = i;	gho[t++].y = j;
				}
			}
		}
		printf ("%d\n", run ());
	}

	return 0;
}

  

转载于:https://www.cnblogs.com/Running-Time/p/4992973.html

内容概要:本文介绍了基于Python实现的SSA-GRU(麻雀搜索算法优化门控循环单元)时间序列预测项目。项目旨在通过结合SSA的全局搜索能力和GRU的时序信息处理能力,提升时间序列预测的精度和效率。文中详细描述了项目的背景、目标、挑战及解决方案,涵盖了从数据预处理到模型训练、优化及评估的全流程。SSA用于优化GRU的超参数,如隐藏层单元数、学习率等,以解决传统方法难以捕捉复杂非线性关系的问题。项目还提供了具体的代码示例,包括GRU模型的定义、训练和验证过程,以及SSA的种群初始化、迭代更新策略和适应度评估函数。; 适合人群:具备一定编程基础,特别是对时间序列预测和深度学习有一定了解的研究人员和技术开发者。; 使用场景及目标:①提高时间序列预测的精度和效率,适用于金融市场分析、气象预报、工业设备故障诊断等领域;②解决传统方法难以捕捉复杂非线性关系的问题;③通过自动化参数优化,减少人工干预,提升模型开发效率;④增强模型在不同数据集和未知环境中的泛化能力。; 阅读建议:由于项目涉及深度学习和智能优化算法的结合,建议读者在阅读过程中结合代码示例进行实践,理解SSA和GRU的工作原理及其在时间序列预测中的具体应用。同时,关注数据预处理、模型训练和优化的每个步骤,以确保对整个流程有全面的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值