当前位置: 首页 > news >正文

有关队列内存超限问题bfs()(待解决)

超限代码:

queue<node> arr;
		node fir{ 0,0,0 };
		arr.push(fir);

		int ex = x - 1, ey = y - 1, ez = z - 1;
		visit[0][0][0] = 1;
		int node1 = 1, k = 0;
		while (!arr.empty() && arr.front().x != ex || arr.front().y != ey || arr.front().z != ez)
		{
			k++;
			int num = 0;
			for (int n = 0; n < node1; n++)
			{
				if (arr.front().x == ex && arr.front().y == ey && arr.front().z == ez)
				{
					while (!arr.empty())	arr.pop();	break;
				}

				// 不是目标,进行标记
				visit[arr.front().x][arr.front().y][arr.front().z] = 1;

				for (int i = 0; i < 6; i++)
				{
					node temp{};
					int tx = arr.front().x + next[i][0],
						ty = arr.front().y + next[i][1],
						tz = arr.front().z + next[i][2];


					// 下标在数组范围内
					// 迷宫中为路
					// 未标记过的有效点
					if (visit[tx][ty][tz] == 0
						&& tx < x && tx >= 0
						&& ty < y && ty >= 0
						&& tz < z && tz >= 0
						&& map[tx][ty][tz] == 0)
					{					
						temp.x = tx;
						temp.y = ty;
						temp.z = tz;
						// 更新最短路

						cout << k << "   " << len[temp.x][temp.y][temp.z] << endl;
						len[temp.x][temp.y][temp.z] = min(len[temp.x][temp.y][temp.z], k);
						cout << k<<"   " << len[temp.x][temp.y][temp.z] << endl;

						//加入有效点
						arr.push(temp);

						// 统计有效节点
						num++;
					}
			}
				// 弹出队列首元素
				arr.pop();
			}
			node1 = num;
		}
		if (len[ex][ey][ez] == inf) cout << -1 << endl;
		else cout << len[ex][ey][ez] << endl;
	}		

不超限代码: 

void bfs()
{
		queue<node> arr;
		node fir{ 0,0,0,0 };
		arr.push(fir);

		int ex = x - 1, ey = y - 1, ez = z - 1;

		visit[0][0][0] = 1;
		int node1 = 1, k = 0;
		
		while (!arr.empty())
		{
			int num = 0;
			if (arr.front().x == ex && arr.front().y == ey && arr.front().z == ez)
			{
				if (arr.front().val <= T)
					cout << arr.front().val << endl;
				else
					cout << -1 << endl;
				return;
			}
			for (int i = 0; i < 6; i++)
			{
				node temp{};
				int tx = arr.front().x + next1[i][0],
					ty = arr.front().y + next1[i][1],
					tz = arr.front().z + next1[i][2];
				// 下标在数组范围内
				// 迷宫中为路
				// 未标记过的有效点
				if (visit[tx][ty][tz] == 0
					&& tx < x && tx >= 0
					&& ty < y && ty >= 0
					&& tz < z && tz >= 0
					&& map[tx][ty][tz] == 0)
				{
					temp.x = tx;
					temp.y = ty;
					temp.z = tz;

					// 更新最短路(以上一级为参考)
					temp.val = arr.front().val + 1;

					// 不是目标,进行标记
					visit[tx][ty][tz] = 1;

					//加入有效点
					arr.push(temp);
				}
			}
			// 弹出队列首元素
			arr.pop();
		}
		cout << -1 << endl;
		return;
}

http://www.dtcms.com/a/3918.html

相关文章:

  • 在 React 中选择使用 JSX 或 JavaScript
  • 2311d游戏引擎适配ios
  • 键盘win键无法使用,win+r不生效、win键没反应、Windows键失灵解决方案(亲测可以解决)
  • Linux基础开发工具之调试器gdb
  • scss 实用教程
  • 深入理解ClickHouse跳数索引
  • Redis之主从复制
  • 搭建神经网络(torch.nn的用法)
  • Llama2通过llama.cpp模型量化 WindowsLinux本地部署
  • Spring Boot自动配置原理、实战、手撕自动装配源码
  • 一步一步详细介绍如何使用 OpenCV 制作低成本立体相机
  • css实现div倾斜效果
  • SDWAN(Software Defined Wide Area Network)概述与优势分析
  • 用于图像处理的高斯滤波器 (LoG) 拉普拉斯
  • hdlbits系列verilog解答(100位BCD加法器)-43
  • 解决win11更新后,文件夹打不开的bug
  • KafkaConsumer 消费逻辑
  • pytorch DistributedDataParallel 分布式训练踩坑记录
  • Clickhouse学习笔记(5)—— ClickHouse 副本
  • 新方向!文心一言X具身智能,用LLM大模型驱动智能小车
  • spring boot security 自定义AuthenticationProvider
  • 软考 系统架构设计师系列知识点之边缘计算(5)
  • 力扣138:随机链表的复制
  • CHATGPT----自然辩证法分析
  • 【Hadoop实战】Hadoop指标系统V2分析
  • Spring Boot 3.0正式发布及新特性解读
  • 文件的想对
  • 竞赛选题 深度学习疲劳驾驶检测 opencv python
  • vue+iView实现下载zip文件导出多个excel表格
  • WebSphere Liberty 8.5.5.9 (二)