C++ 利用栈实现走迷宫
来源:互联网 发布:白俄罗斯交友软件 编辑:程序博客网 时间:2024/06/05 00:23
使用两个栈,一个用于初始化迷宫,一个用于回溯。
#include <iostream>#include <string>#include <cstring>#include <stack>using namespace std;template<class T>class Stack:public stack<T>{public: T pop(){ T tmp = stack<T>::top(); stack<T>::pop(); return tmp; }};class Cell{private: int x, y; friend class Maze;public: Cell(int i = 0, int j = 0){ x = i; y = j; } bool operator==(const Cell &c)const{ return x == c.x && y == c.y; }};class Maze{private: Cell currentCell; //当前位置 Cell exitCell; //出口位置 Cell entryCell; //初始位置 const char exitMarker, entryMarker, visited, passage, wall; //描述迷宫的变量 Stack<Cell> mazeStack; //迷宫 char **store; void pushUnvisited(int row, int col); friend ostream &operator<<(ostream &out, const Maze &maze); int rows, cols;public: Maze(); void exitMaze();};Maze::Maze():exitMarker('e'), entryMarker('m'), visited('.'), passage('0'), wall('1'){ Stack<char *> mazeRows; char str[80], *s; int col, row = 0; cout << "enter a regular maze using the following " << "characters:\nm - entry\ne - entry\ne - exit\n1 - wall\n0 - passage\n" << "enter one line at a time; end with ctrl-z:\n"; while(cin >> str){ row++; cols = strlen(str); s = new char[cols + 3]; mazeRows.push(s); strcpy(s+1, str); s[0] = s[cols + 1] = wall; s[cols + 2] = '\0'; if(strchr(s, exitMarker) != 0){ //寻找出口位置 exitCell.x = row; exitCell.y = strchr(s,exitMarker) - s; } if(strchr(s, entryMarker) != 0){ //寻找初始位置 entryCell.x = row; entryCell.y = strchr(s, entryMarker) - s; } } rows = row; store = new char *[rows+2]; store[0] = new char[cols+3]; for( ; !mazeRows.empty(); row--) store[row] = mazeRows.pop(); store[rows+1] = new char[cols+3]; store[0][cols+2] = store[rows+1][cols+2] = '\0'; for(col = 0; col <= cols+1; col++){ store[0][col] = wall; store[rows+1][col] = wall; }}void Maze::pushUnvisited(int row, int col){ if(store[row][col] == passage || store[row][col] == exitMarker) mazeStack.push(Cell(row, col));}void Maze::exitMaze(){ int row, col; currentCell = entryCell; while(!(currentCell == exitCell)){ row = currentCell.x; col = currentCell.y; cout << *this; if(!(currentCell == entryCell)) store[row][col] = visited; pushUnvisited(row-1,col); pushUnvisited(row+1,col); pushUnvisited(row,col-1); pushUnvisited(row,col+1); if(mazeStack.empty()){ cout << *this; cout << "failure\n"; return ; } else currentCell = mazeStack.pop(); } cout << *this; cout << "success\n";}ostream & operator<<(ostream &out, const Maze &maze){ for(int row = 0; row <= maze.rows+1; row++) out << maze.store[row] << endl; out << endl; return out;}int main(){ Maze().exitMaze(); return 0;}
有空补上解析
阅读全文
0 0
- 【c++】利用循环和栈实现走迷宫
- 【c++】利用递归实现走迷宫
- C++ 利用栈实现走迷宫
- 老鼠走迷宫 (栈实现)
- 用栈实现走迷宫
- 用C语言实现走迷宫
- 用栈实现的自动走迷宫
- c++实现走迷宫
- java实现走迷宫
- DFS实现走迷宫
- 【数据结构】走迷宫实现
- 计算机走迷宫 C源码
- C语言走迷宫源代码
- c语言 走迷宫问题
- 老鼠走迷宫(c/python)
- 栈应用-走迷宫
- java实现走迷宫算法
- 老鼠走迷宫问题(C++栈实现)
- Android Studio3.0新特性
- Java的ClassLoader加载机制
- RxJava + Retrofit
- spring @Autowired @Quatifier @Resource的使用
- Ubuntu环境下挂载新硬盘
- C++ 利用栈实现走迷宫
- Netty实战读书笔记(第六章)
- C++中的随机数知识总结
- qt下载资源
- Spring整合Mybatis(一)------MapperScannerConfigurer
- 如何理解虚拟DOM
- 系统内置ContentProvider
- UDF函数
- ARKit从入门到精通(1)-ARKit初体验