当前位置: 首页 > 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.hskmm.com/?act=detail&tid=36585

相关文章:

  • WPF使用MediaCapture开发相机应用(四、相机录视频)
  • 链队
  • Gitee本土化战略深度解析:中国开发者生态的合规与效率革命
  • 2025年10月上海装修公司口碑榜:十强对比评测
  • 02-GPIO-铁头山羊STM32标准库新版笔记
  • 【多校支持、EI检索】第六届大数据与社会科学国际学术会议(ICBDSS 2025)
  • IDC iPaaS市场报告解读:独立厂商与云巨头的“双轨竞速”
  • 2025年10月仓储管理系统推荐:鸿链云仓领衔五大方案对比评测榜
  • 2025年10月电动叉车销售公司排行榜:五家主流服务商对比评测
  • 2025年口罩机厂家权威推荐榜:全自动口罩机器,全自动KN95口罩机源头企业综合评测与采购指南
  • 2025年包装机厂家权威推荐榜单:全自动包装机/包装生产线/非标定制机器与生产线专业选购指南
  • Timing Signoff 技术精要
  • Oracle故障处理:10G RAC srvctl注册实例正常,但是crs切不能管理实例
  • 杂题选做-2
  • 读书笔记:白话解读Oracle范围分区
  • 2025年10月人形机器人场景落地商评测榜:赛飞特工程技术集团数据透视
  • 科林电气与利驰软件续签合作,共启数字化协同新篇章!
  • 详细介绍:资产信息收集与指纹识别:HTTPX联动工具实战指南
  • 易基因:剑桥大学团队利用微量WGBS等揭示DNMT3L在胎盘发育中的DNA甲基化调控机制:CSC(IF20.5)
  • 10.22
  • 和橘子学AI创作【500集120实战】
  • iOS 26 性能调试工具全景指南 多工具组合 + 实战流程
  • 102302134陈蔡裔数据采集第一次作业
  • 2025年10月蒸汽发生器品牌榜:辰能能源领衔五强对比
  • 2025吹塑机厂家权威推荐:鼎浩包装科技实力企业,专业定制高效生产方案
  • 2025年10月蒸汽发生器品牌评测榜:节能与合规全解析
  • 活动邀请丨2025 全球机器学习技术大会
  • 2025年10月低空经济核心公司对比评测榜:五强排名与全维度数据解析
  • 01-准备-铁头山羊STM32标准库新版笔记
  • platformio上ESP32-s3,N16R8选择板子的解决方案