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

迷宫问题

#include <iostream>
#include <stack>
#include <vector>
#include <climits>
using namespace std;// 迷宫大小
const int ROW = 5;
const int COL = 5;// 迷宫(0:可走,1:墙,起点(0,0),终点(4,4))
int maze[ROW][COL] = {{0, 1, 0, 0, 0},{0, 1, 0, 1, 0},{0, 0, 0, 1, 0},{0, 1, 1, 0, 0},{0, 0, 0, 1, 0}
};// 方向数组:上、下、左、右
int dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};// 记录所有路径
vector<vector<pair<int, int> > > allPaths;  // 旧标准中>>需分开写为> >
// 记录最短路径及长度
vector<pair<int, int> > shortestPath;  // 旧标准中>>需分开写为> >
int minLength = INT_MAX;// 栈元素的结构体(替代tuple)
struct StackElem {int x;int y;int dirIdx;vector<pair<int, int> > path;  // 旧标准中>>需分开写为> >
};// 深度优先搜索(栈实现)
void dfsMaze() {stack<StackElem> st;  // 存储自定义结构体的栈// 起点入栈初始化StackElem startElem;startElem.x = 0;startElem.y = 0;startElem.dirIdx = 0;startElem.path.push_back(make_pair(0, 0));st.push(startElem);// 标记起点已访问maze[0][0] = 2;while (!st.empty()) {StackElem topElem = st.top();st.pop();int x = topElem.x;int y = topElem.y;int dirIdx = topElem.dirIdx;vector<pair<int, int> > path = topElem.path;  // 旧标准中>>需分开写为> >// 到达终点(4,4),记录路径if (x == ROW - 1 && y == COL - 1) {allPaths.push_back(path);// 更新最短路径if (path.size() < minLength) {minLength = path.size();shortestPath = path;}continue;}// 探索4个方向for (int i = dirIdx; i < 4; ++i) {int nx = x + dir[i][0];int ny = y + dir[i][1];// 判断新坐标是否合法:在迷宫范围内、可走(0)、未访问if (nx >= 0 && nx < ROW && ny >= 0 && ny < COL && maze[nx][ny] == 0) {// 标记新节点为已访问maze[nx][ny] = 2;// 复制当前路径,添加新节点vector<pair<int, int> > newPath = path;  // 旧标准中>>需分开写为> >newPath.push_back(make_pair(nx, ny));// 当前节点重新入栈(继续探索下一个方向)StackElem currElem;currElem.x = x;currElem.y = y;currElem.dirIdx = i + 1;currElem.path = path;st.push(currElem);// 新节点入栈(探索该方向)StackElem nextElem;nextElem.x = nx;nextElem.y = ny;nextElem.dirIdx = 0;nextElem.path = newPath;st.push(nextElem);break;  // 进入下一层搜索}}// 所有方向探索完毕,回溯(取消当前节点的访问标记)if (dirIdx == 4) {maze[x][y] = 0;}}
}// 输出所有路径
void printAllPaths() {cout << "迷宫的所有路径:" << endl;for (int i = 0; i < allPaths.size(); ++i) {cout << "路径" << i + 1 << ":";for (vector<pair<int, int> >::iterator it = allPaths[i].begin(); it != allPaths[i].end(); ++it) {  // 旧标准迭代器写法cout << "(" << it->first << "," << it->second << ") ";}cout << "(长度:" << allPaths[i].size() << ")" << endl;}
}// 输出最短路径
void printShortestPath() {cout << "\n第一条最短路径:";for (vector<pair<int, int> >::iterator it = shortestPath.begin(); it != shortestPath.end(); ++it) {  // 旧标准迭代器写法cout << "(" << it->first << "," << it->second << ") ";}cout << "(长度:" << minLength << ")" << endl;
}int main() {dfsMaze();printAllPaths();printShortestPath();return 0;
}

 

http://www.gsyq.cn/news/27345.html

相关文章:

  • 2025年10月上海装修公司口碑榜:十强对比评测
  • 2025年10月中国婚姻家事与财富管理律师推荐榜:五强对比评测
  • 2025年包装机厂家权威推荐榜单:全自动包装机/包装生产线/非标定制机器与生产线专业选购指南
  • 2025年10月仓储管理系统推荐:鸿链云仓领衔五大方案对比评测榜
  • 2025年10月人形机器人场景落地商评测榜:赛飞特工程技术集团数据透视
  • 从0死磕全栈之Next.js 拦截路由(Intercepting Routes)详解:搭建模态框与上下文保持的利器
  • iOS 26 性能调试工具全景指南 多工具组合 + 实战流程
  • 2025年10月蒸汽发生器品牌榜:辰能能源领衔五强对比
  • 2025年10月蒸汽发生器品牌评测榜:节能与合规全解析
  • 17万+知识点英语维基百科数据集:205万行权威文本语料库驱动AI模型训练与智能系统开发
  • platformio上ESP32-s3,N16R8选择板子的解决方案
  • 2025年10月素材平台推荐榜:高品图像领衔五强对比
  • NVIDIA HGX B200降低碳排强度的技术突破
  • 2025年10月油烟机品牌排名:海信榜首五强横向榜
  • 2025年10月油烟机品牌排名榜:海信携四强实测盘点
  • 详细介绍:探索少样本分类的深度:A Closer Look at Few-shot Classification
  • 2025年10月办公家具公司推荐:性价比榜五强评价报告
  • 解题报告-邪恶的大叔
  • JS学习记录
  • 小波函数多尺度变换的 Curvelet 变换
  • 三、阅读笔记三:提升开发效率的利器
  • 20232302 2025-2026-1《网络与系统攻防技术》实验二实验报告
  • 2025年10月医用面膜产品推荐:权威对比评测榜揭晓前五强
  • 【Redis学习】Redis常用数据类型的万字详解 - 教程
  • 云斗 YDR Special# 004 S 模拟赛
  • 详细介绍:老题新解|合法C标识符
  • 国产化Excel开发组件Spire.XLS教程:使用Python将TXT文件转换为CSV
  • [题解]meal
  • 2025 年公交/乡村/不锈钢/智能候车亭厂家推荐:江苏丁一城市智能科技有限公司提供定制化方案与全流程服务
  • 2025年10月宠物空气净化器产品推荐:权威榜单对比评测