栈的应用———迷宫问题
来源:互联网 发布:匡威帆布鞋淘宝店代理 编辑:程序博客网 时间:2024/06/06 03:20
一、简单的有一条活路的迷宫
算法描述:
#include <iostream>#include <stack>using namespace std;struct Seat // 坐标位置{ Seat(int x, int y) :_x(x) , _y(y) {} int _x; int _y;};#define ROW 10#define COL 10class Masze{public: Masze(int map[][COL]) { for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL; ++j) { _map[i][j] = map[i][j]; } } } bool IsPass(const Seat& s) //判断下一步是否能走 { if ((_map[s._x][s._y] == 1)|| IsExit(s)) return true; return false; } bool IsExit(const Seat& s) // 判断是否为出口 { if (s._x < 0 || s._x >= ROW || s._y < 0 || s._y >= COL) { return true; } return false; } bool PassMaze(Seat& enter) // 迷宫是否能走通 { if (_map[enter._x][enter._y] != 1) { return false; } stack<Seat> s; s.push(enter); while (!s.empty()) { Seat Cur = s.top(); if (IsExit(Cur)) { return true; } _map[Cur._x][Cur._y] = 2; Seat up(Cur._x-1, Cur._y); if (IsPass(up)) { s.push(up); continue; } Seat left(Cur._x , Cur._y - 1); if (IsPass(left)) { s.push(left); continue; } Seat right(Cur._x, Cur._y + 1); if (IsPass(right)) { s.push(right); continue; } Seat down(Cur._x + 1, Cur._y); if (IsPass(down)) { s.push(down); continue; } _map[Cur._x][Cur._y] = 3; s.pop(); } return false; } void PrintMap() { for (int i = 0; i < ROW; ++i) { for (int j = 0; j < COL; ++j) { cout << _map[i][j] << " "; } cout << endl; } cout << endl; }private: int _map[ROW][COL];};int main(){ int arr[ROW][COL] = { { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 } }; Masze m(arr); Seat enter(9, 5); m.PassMaze(enter); m.PrintMap(); return 0;}
运行之:
二。多条通路最短路问题
#include <stack>#include <iomanip>#include <vector>using namespace std;struct Seat // 位置坐标{ Seat(int x, int y) :_x(x) , _y(y) {} bool operator==(const Seat& s) { return (_x == s._x) && (_y == s._y); } bool operator!=(const Seat& s) { return !(*this == s); } int _x; int _y;};class Masze{public: Masze(int* map, int row, int col) //利用vector创建二维数组 :_row(row) , _col(col) { _map.resize(row); // 给出行 /*_map = new int*[row];*/ for (int i = 0; i < row; ++i) { _map[i].resize(col); //每一行的的列 /*_map[i] = new int[col];*/ for (int j = 0; j <_col; ++j) { _map[i][j] = map[col*i + j]; } } } bool IsPass(const Seat& cur, const Seat& next) //判断下一步可不可以走 { /*if (IsExit(cur)) return false;*/ if (_map[next._x][next._y] == 0) // 0 为死路 return 0; if ((_map[next._x][next._y] == 1)) // 1 为活路 return true; else if ((_map[cur._x][cur._y] < _map[next._x][next._y]) && (_map[cur._x][cur._y] + 1 != _map[next._x][next._y])) return true; // 当前步小于下一步可以走与不是走过的路 return false; } bool IsExit(const Seat& s) { if (s._x == 0 || s._x == _row - 1 || s._y == 0 || s._y == _col - 1) // 判断是不是出口 { return true; } return false; } bool PassMaze(Seat& enter) // 迷宫是否能走出去 { if (_map[enter._x][enter._y] != 1) //检测入口 { return false; } stack<Seat> s; // 保存走过每一步 stack<Seat> shortpath; //保存最短路 s.push(enter); bool flag = false; while (!s.empty()) // 最后从入口点退出 { Seat Cur = s.top(); if (flag && IsExit(Cur)) // 第一步不是出口 && 判断出口 { if (Cur != enter) // 最后为入口点 出口不等于入口 if (shortpath.empty() || (s.size() < shortpath.size())) // 将最短路放在shortpatn里 { shortpath = s; } if (!s.empty()) s.pop(); // 到出口退栈 continue; } if (!flag && Cur == enter) // 先走第一步 但不是最后一步 { _map[Cur._x][Cur._y] = _map[Cur._x][Cur._y] + 1; //下一步为当前步加1 flag = true; } Seat up(Cur); up._x -= 1; if (IsPass(Cur ,up)) // 向上走 { _map[up._x][up._y] = _map[Cur._x][Cur._y] + 1; s.push(up); continue; } Seat left(Cur._x, Cur._y - 1); // 向左走 if (IsPass(Cur, left)) { _map[left._x][left._y] = _map[Cur._x][Cur._y] + 1; s.push(left); continue; } Seat right(Cur._x, Cur._y + 1); //向右走 if (IsPass(Cur, right)) { _map[right._x][right._y] = _map[Cur._x][Cur._y] + 1; s.push(right); continue; } Seat down(Cur._x + 1, Cur._y); //向下走 if (IsPass(Cur ,down)) { _map[down._x][down._y] = _map[Cur._x][Cur._y] + 1; s.push(down); continue; } s.pop(); // 上下左右都不能走退栈 } if (!shortpath.empty()) return true; return false; } void PrintMap() { for (int i = 0; i < _row; ++i) { for (int j = 0; j < _col; ++j) { cout << setw(5)<<left<<_map[i][j] ; } cout << endl; } cout << endl; }private: vector<vector<int>> _map; int _row; int _col;};int main(){ int arr[10][10] = { { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 } }; Masze m((int*)arr, 10, 10); Seat enter(9, 5); cout << m.PassMaze(enter) << endl; m.PrintMap(); return 0;}
运行之:
阅读全文
0 0
- 栈的应用——迷宫问题
- 迷宫问题——栈的应用
- 栈的应用——迷宫问题
- 栈的应用———迷宫问题
- 栈的应用——迷宫求解问题
- 栈的应用——求解迷宫问题
- 栈的应用——迷宫
- java简单的迷宫问题——堆栈的应用
- 队列的应用——求解迷宫问题
- 迷宫问题(栈的应用)
- 栈的应用--迷宫问题
- 迷宫问题——栈
- 迷宫问题(栈的应用,二维数组表示迷宫)
- 【栈应用】迷宫问题
- 栈应用----迷宫问题
- <数据结构>栈的应用三:迷宫问题
- 栈的应用之迷宫问题
- 栈的应用-迷宫问题-数据结构
- Codeforces Round #447 A. QAQ
- Mybatis之Mybatis框架原理(2)
- Python-Numpy函数-tile函数
- git rebase简介(基本篇)-20171119
- 之前看到的一个思维题 《生日问题》
- 栈的应用———迷宫问题
- 面向对象,类,多态,继承,常用关键字,异常
- Java环境变量配置问题:路径包含空格或特殊字符(_,~等)
- 电路加法初探
- 前端构建工具gulpjs的使用介绍及技巧
- 机器学习入门学习笔记:(4.2)核函数和软间隔
- JavaShowAlgorithm-优化的插入排序带着二分查找
- NetBeans工具学习之道:NetBeans IDE Java 快速入门教程
- MySQL索引类型总结和使用技巧以及注意事项