数据结构迷宫问题C++实现

来源:互联网 发布:玩名堂换域名吗? 编辑:程序博客网 时间:2024/06/07 14:53

出现实现图:



.h文件实现堆栈简易操作(此处没有将声明与实现分离,应注意!)

#pragma warning (disable : 4715)#ifndef  STACK_H#define STACK_Hstruct Position//结构体位置,表示迷宫每一格的行号和 列号{int row;//行号int col;//列号};class Stack{public:Stack(int MaxSize = 10);~Stack() { delete[] Element; };bool IsEmpty() const { return top == -1; }bool IsFull() const { return top == Maxtop; }Position Top() const;//返回栈顶元素Stack& Add(Position &x);//添加元素Stack& Delete(Position &x);//删除元素private:int top;//栈顶,入栈和出栈的地方。int Maxtop;//栈顶元素位置。Position *Element;};Stack::Stack(int MaxSize){Maxtop = MaxSize - 1;Element = new Position[MaxSize];top = -1;}Position Stack::Top() const{if (IsEmpty()){}elsereturn Element[top];}Stack& Stack::Add(Position &x){if (IsFull()){}else{++ top;Element[top].row = x.row;Element[top].col = x.col;}return *this;}Stack& Stack::Delete(Position &x){if (IsEmpty()){}else{x.row = Element[top].row;x.col = Element[top].col;--top;}return *this;}#endif 

源文件:

#include<iostream>#include"STack.h"using namespace std;int main(){cout << "老鼠迷宫!" << endl;//构建迷宫。maze[1][1]表示入口, maze[10][10]表示出口。char **maze = new char *[12];for (int i = 0; i < 12; ++i)maze[i] = new char[12];//给迷宫加“墙”。for (int i = 0; i < 12; ++i){maze[11][i] = '+';maze[0][i] = '+';maze[i][0] = '+';maze[i][11] = '+';}//构建迷宫,用符号+表示墙壁,空格表示通路,以使迷宫尽可能好看!for (int a = 1; a < 11; ++a)for (int b = 1; b < 11; ++b)maze[a][b] = ' ';for (int i = 2; i < 7; ++i)maze[1][i] = '+';maze[2][6] = maze[2][8] = maze[3][4] = maze[3][6] = maze[4][2] = maze[4][4] = maze[4][6] = maze[4][8] = maze[4][9] = '+';maze[5][2] = maze[5][4] = maze[5][6] = maze[5][8] = maze[6][2] = maze[6][3] = maze[6][4] = maze[6][6] = maze[6][8] = maze[6][10] = '+';maze[7][2] = maze[7][6] = maze[7][8] = maze[7][10] = maze[8][2] = maze[8][4] = maze[8][5] = maze[8][6] = maze[8][8] = '+';maze[9][1] = maze[9][8] = maze[10][5] = maze[10][6] = maze[10][7] = maze[10][8] = '+';//输出迷宫 布局。cout << "迷宫如下所示:" << endl;for (int a = 0; a < 12; ++a){for (int b = 0; b < 12; ++b)cout << maze[a][b] << " ";cout << endl;}//构建迷宫完毕,开始寻找路径。Stack* Path = new Stack(10*10 - 1);//栈用来储存路径以及遇到障碍物时方标返回上一步。Position offset[4];//设置模拟移动方法。offset[0].row = 0; offset[0].col = 1;//右移offset[1].row = 1; offset[1].col = 0;//下移offset[2].row = 0; offset[2].col = -1;//左移offset[3].row = -1; offset[3].col = 0;//上移Position here;//代表当前位置。here.col = 1;here.row = 1;maze[1][1] = '#';//入口堵住,防止返回入口。int option = 0;//上、下、左、右四种走法。记录走的次数,一共四次,上下左右分布探索。int Lastoption = 3;//模拟移动,开始寻找出口。while (here.row != 10 || here.col != 10){int x, y;while (option <= Lastoption)//最多循环4次,尝试每种走法。{x = here.row + offset[option].row;y = here.col + offset[option].col;if (maze[x][y] == ' ')//只要寻找得到通路,就退出循环。break;option++;}if (option <= Lastoption){//移动一次,是通路,此处位置入栈,移动数option归0。Path->Add(here);here.row = x;here.col = y;maze[x][y] = '*';//将该处修改为墙壁,防止返回,做重复工作。option = 0;}else//不是通路,会退一步,路径记录栈栈顶元素出栈。{if (Path->IsEmpty())//出现空栈,表明该迷宫没有出口!cout << "这个迷宫没有出口!" << endl;Position next;next.col = next.row = 0;Path->Delete(next);//新的位置,表示here的前一步,用来回退。if (next.row == here.row)option = 2 + next.col - here.col;//通过计算得出回退后应该执行哪一种走法!elseoption = 3 + next.row - here.row;here = next;}}cout << "老鼠找到了迷宫的出口!!" << endl;//以图形的样式输出寻找得到的路径。for (int i = 0; i < 12; ++i){for (int a = 0; a < 12; ++a)cout << maze[i][a] << " ";cout << endl;}system("pause");}              



原创粉丝点击