问题 R: 胜利大逃亡(HUST)

复制代码
#include <deque>
#define inf 200000
#include<iostream>
#include<queue>
using namespace std;

// 迷宫坐标
int map[59][59][59] = { 0 };

// 可访问标记
int visit[51][51][51] = { 0 };	

// 移动方式
int next1[7][4] = { {1,0,0},{-1,0,0}
				,  {0,1,0},{0,-1,0}
				,  {0,0,1},{0,0,-1} };

// 声明迷宫大小,及时间
int x = 0, y = 0, z = 0, T = 0;

// 判断能否逃出迷宫
int f = 0;

// bfs
void bfs();
struct node {
	int x;
	int y;
	int z;
	int val;
};
int main()
{

	int k = 0; scanf("%d", &k);
	while (k--)
	{
		// 输入 x高, y长 ,z长
		scanf("%d %d %d %d", &x, &y, &z, &T);

		// 输入立体迷宫
		for (int x1 = 0; x1 < x; x1++)
		{
			for (int y1 = 0; y1 < y; y1++)
			{
				for (int z1 = 0; z1 < z; z1++)
				{	visit[x1][y1][z1] = 0;
					scanf("%d", &map[x1][y1][z1]);}
			}
		}
		// 队列bfs
		bfs();		
	}
	return 0;
}
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;
}
相关推荐
乙己4079 分钟前
设计模式——单例模式(singleton)
java·c++·单例模式·设计模式
CoovallyAIHub31 分钟前
视觉语言模型(VLM)深度解析:如何用它来处理文档?
深度学习·算法·计算机视觉
嵌入式小李.man1 小时前
linux中多路复用IO:select、poll和epoll
linux·c++
CoovallyAIHub1 小时前
估值百亿独角兽创始人硕士论文曝光!宇树科技王兴兴的“性价比”思维10年前就已注定
深度学习·算法·计算机视觉
郝学胜-神的一滴1 小时前
QAxios研发笔记(二):在Qt环境下基于Promise风格简化Http的Post请求
开发语言·c++·笔记·qt·网络协议·程序人生·http
敲代码的嘎仔1 小时前
数据结构算法学习day3——二分查找
java·开发语言·数据结构·学习·程序人生·算法·职场和发展
代码不停1 小时前
JavaEE多线程进阶
java·数据结构·java-ee
晨非辰1 小时前
《数据结构风云》:二叉树遍历的底层思维>递归与迭代的双重视角
数据结构·c++·人工智能·算法·链表·面试
小白菜又菜2 小时前
Leetcode 495. Teemo Attacking
算法·leetcode·职场和发展
杨福瑞4 小时前
数据结构:单链表(1)
c语言·开发语言·数据结构