迷宫问题——堆栈应用(C++版)
2015-06-11 20:27
507 查看
1.设计一个坐标类:
2.设计一个堆栈,存储坐标点:
3.设计求解迷宫问题类:
4.主函数
class mazepoint { friend class mazestack; public: mazepoint(){} mazepoint(int a, int b) { x = a; y = b; } ~mazepoint(){} private: int x; int y; mazepoint* next; };
2.设计一个堆栈,存储坐标点:
//定义一个栈,可以将坐标放进栈中 class mazestack { public: mazestack() { length = 0; top = NULL; } ~mazestack(){} void push(const mazepoint* item) { if (top == NULL) { mazepoint* newnode = new mazepoint; newnode->x = item->x; newnode->y = item->y; newnode->next = NULL; top = newnode; ++length; } else { mazepoint* newnode = new mazepoint; newnode->x = item->x; newnode->y = item->y; newnode->next = top; top = newnode; ++length; } } void pop() { if (top == NULL) { cout << "栈为空" << endl; exit(1); } else { mazepoint* temp = top; top = top->next; delete temp; --length; } } bool empty() { return top == NULL; } private: int length; mazepoint* top; };
3.设计求解迷宫问题类:
//以后建立动态数组存储迷宫数据 class mazerun { public: mazerun() { m = 7; int (*maze)[7] = new int[7][7]; } ~mazerun(){} void createmaze() //产生一个m*m的随机整数迷宫 { //将迷宫最外层添加一层墙壁,值为1 for (int i = 0; i < m ; ++i) { maze[i][0] = maze[i][m-1] = 1; maze[0][i] = maze[m-1][i] = 1; } //用随机数填充迷宫:0表示可通路,1表示障碍,2表示已经走过路,3表示可通路径不正确点 cout << "请输入迷宫m-1*m-1,并使迷宫存在一条通路" << endl; // cout << "默认情况下:第一个点和最后一个点为进,出口,值为0" << endl; int num=0; int cc = 0; vector<int> vect; cout << "输入迷宫数" << endl; while (cin >> num) { vect.push_back(num); } for (int i = 1; i < m - 1; ++i) { for (int j = 1; j < m - 1; ++j) { maze[i][j] = vect[cc++]; } } maze[1][1] = 0; //入口 maze[m-2][m-2] = 0; //出口 //输出完整的迷宫 cout << "完整的迷宫:" << endl; for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) cout << maze[i][j]<<" "; cout << endl; } } bool findpath() { mazepoint* start = new mazepoint(1, 1); //起点 // mazepoint* end = new mazepoint(m - 2, m - 2);//终点 mazestack mazepath; mazepath.push(start);//将起始点压入路径栈中 bool flag = 1; //当找到出口时置为0 int x = 1, y = 1; maze[1][1] = 1; //阻止返回入口 //从上下左右四个方向找出可通过的路径 while (flag) //当找不到出口时,循环结束 { if (maze[x - 1][y] == 0) { maze[x - 1][y] = 2; mazepath.push((new mazepoint(x - 1, y))); //设置当前点为新起点 x = x - 1; y = y; } else if (maze[x +1][y] == 0) { maze[x +1][y] = 2; mazepath.push((new mazepoint(x + 1, y))); //设置当前点为新起点 x = x + 1; y = y; } else if (maze[x ][y-1] == 0) { maze[x ][y-1] = 2; mazepath.push((new mazepoint(x , y-1))); //flag = 1; //设置当前点为新起点 x = x; y = y - 1; } else if (maze[x ][y+1] == 0) { maze[x ][y+1] = 2; mazepath.push((new mazepoint(x , y+1))); // flag = 1; //设置当前点为新起点 x = x; y = y + 1; } //判断是否走到出口,是否将出口设为起点 if ((x == m - 2) && (y == m - 2)) { cout << "迷宫出口已找到" << endl; flag = 0; return 1; } if ((maze[x - 1][y] != 0) && (maze[x + 1][y] != 0) && (maze[x][y - 1] != 0) && (maze[x][y + 1] != 0)) { if (maze[x - 1][y] == 2) { maze[x][y] = 3; mazepath.pop(); // flag = 1; //设置当前点为新起点 x = x - 1; y = y; } else if (maze[x + 1][y] == 2) { maze[x][y] = 3; mazepath.pop(); //flag = 1; //设置当前点为新起点 x = x + 1; y = y; } else if (maze[x][y - 1] == 2) { maze[x][y] = 3; mazepath.pop(); //flag = 1; //设置当前点为新起点 x = x; y = y - 1; } else if (maze[x][y + 1] ==2) { maze[x][y] = 3; mazepath.pop(); //flag = 1; //设置当前点为新起点 x = x; y = y + 1; } else if ((maze[x - 1][y] != 2) && (maze[x + 1][y] != 2) && (maze[x][y - 1] != 2) && (maze[x][y + 1] != 2)) { cout << "迷宫无路可走" << endl; flag = 0; return 0; } } } } void printpath() { if (findpath()) { maze[1][1] = 2; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (maze[i][j] == 2) cout << '*' << " "; else if (maze[i][j] == 3) cout << 0 << " "; else cout << 1 << " "; } cout << endl; } } else { cout << "迷宫没有通路,不输出路径" << endl; } } private: int m; int maze[10][10]; };PS: 以上是定义在头文件maze.h中。
4.主函数
#include<iostream> #include"maze.h> int main() { mazerun run; run.createmaze(); run.printpath(); system("pause"); return 0; }5.结果:
相关文章推荐
- 给四条线段,判断是否是矩形
- 类作用域中名字查找的思考(《C++ primer》第四版P382)
- 【学习笔记】【C语言】循环结构-while
- 用strlen函数计算字符串的长度(C语言)
- C++ Const详解,包含类对象的类
- c++ 使用throw抛出异常
- C++写文件时覆盖与否的问题
- C++数据在内存中的存放的三个区域全局数据区,堆和栈
- C++ STL之list详解
- C++ 三大函数该写就要写
- leetcode 日经贴,Cpp code -Basic Calculator
- 简单栈的c语言实现
- 对VC++下Debug模式和Release模式的简要分析
- LeetCode 之 Pascal's Triangle — C++ 实现
- LeetCode 之 Merge Sorted Array — C++ 实现
- 行参和实参的区别 原文说的是c++的 借鉴一下吧
- 行参和实参的区别 原文说的是c++的 借鉴一下吧
- C++学习笔记(3) 数组
- 类型转换
- 提高C++程序运行效率的10个简单方法