利用自定义栈实现的走迷宫程序_C++
利用自定义栈实现的走迷宫程序_C++
- 程序纲要
- 自定义栈
- 寻路类
- 完整实现
- 最后
程序纲要 程序利用了栈先进后出的特点,相比用数组,简化了设计程序的思考过程
利用寻路函数可以判断前进后退,方向等等
自定义栈
typedef struct Position
{
int x;
int y;
bool operator == (Position &a)
{
if (this->x == a.x && this->y == a.y)
return true;
else
return false;
}
}Position;
typedef struct Pos
{
Position P;
Pos *Next;
}Pos;
class Stack
{
private:
Pos *Head;
Pos *End;
public:
Stack();
~Stack();
bool IsEmpty() { return Head == nullptr;
}
Pos GetTop();
void Push(Pos New);
void Push(Position New);
void Pop();
};
Position和Pos为链表栈的链表结构
Stack类为自定义的栈,Head为栈底,End为栈顶
IsEmpty判断栈是否为空
GetTop获得栈顶的元素
Push将元素压入栈,两个重载函数压入的类型不一样
Pop删除栈顶的元素
寻路类
class Find
{
private:
Stack stack;
//stack类
Position Start;
//起点
Position Target;
//终点
int maze[Maze_Rows][Maze_Cols];
//存放迷宫的数组
public:
Find(int maze[][10],Position Start,Position Target);
~Find();
Position Next_Step();
void Go_Next(Position Next);
void Go_Back();
bool IsArrive();
bool AutoFind();
};
Next_Step为判断在该位置下一步的坐标
Go_Next走向给定的坐标
Go_Back原路返回一格
IsArrive判断是否到达
AutoFind调用直接寻路
在走下一步的时候,要在刚刚的地方放“障碍物”,以防会绕回来造成死循环
退一步的时候就不必把放的“障碍物”拿掉,因为防止以后又走到死胡同里面去
完整实现
//Find.cppconst int Maze_Rows = 10;
//迷宫的行数
const int Maze_Cols = 10;
//迷宫的列数Find::Find(int maze[][10], Position Start, Position Target)
{
for(int i = 0;
i < Maze_Rows;
i++)
for (int j = 0;
j < Maze_Cols;
j++)
{
this->maze[i][j] = maze[i][j];
}
this->Start = Start;
this->Target = Target;
stack.Push(Start);
}Find::~Find()
{
}Position Find::Next_Step()
{
Pos Now = stack.GetTop();
if (maze[Now.P.y][Now.P.x + 1] != 1)
{
Now.P.x = Now.P.x + 1;
return Now.P;
}
else if (maze[Now.P.y + 1][Now.P.x] != 1)
{
Now.P.y = Now.P.y + 1;
return Now.P;
}
else if (maze[Now.P.y][Now.P.x - 1] != 1)
{
Now.P.x = Now.P.x - 1;
return Now.P;
}
else if (maze[Now.P.y - 1][Now.P.x] != 1)
{
Now.P.y = Now.P.y - 1;
return Now.P;
}
else
{
return Now.P;
//返回原地,表示没有路可走
}
}void Find::Go_Next(Position Next)
{
Pos Now = stack.GetTop();
maze[Now.P.y][Now.P.x] = 1;
stack.Push(Next);
}void Find::Go_Back()
{
Pos Now = stack.GetTop();
maze[Now.P.y][Now.P.x] = 1;
stack.Pop();
}bool Find::IsArrive()
{
if (Target == stack.GetTop().P)
return true;
else
return false;
}bool Find::AutoFind()
{
while (!IsArrive())
{
Position Next = Next_Step();
Position Now = stack.GetTop().P;
if (Next == Now)
{
Go_Back();
}
else
{
Go_Next(Next_Step());
}
std::cout << stack.GetTop().P.x << "," << stack.GetTop().P.y << std::endl;
}
return true;
}
//Stack.cpp
Stack::Stack()
{
Head = End = nullptr;
}Stack::~Stack()
{
delete Head;
}Pos Stack::GetTop()
{
return *End;
}void Stack::Push(Pos New)
{
if (IsEmpty())
{
Head = new Pos;
Head->P.x = New.P.x;
Head->P.y = New.P.y;
Head->Next = nullptr;
End = Head;
}
else
{
End->Next = new Pos;
End = End->Next;
End->P.x = New.P.x;
End->P.y = New.P.y;
End->Next = nullptr;
}
}void Stack::Push(Position New)
{
if (IsEmpty())
{
Head = new Pos;
Head->P.x = New.x;
Head->P.y = New.y;
Head->Next = nullptr;
End = Head;
}
else
{
End->Next = new Pos;
End = End->Next;
End->P.x = New.x;
End->P.y = New.y;
End->Next = nullptr;
}
}void Stack::Pop()
{
if (IsEmpty())
{ }
else
{
Pos *P = Head;
while (P->Next != End)
{
P = P->Next;
}
delete End;
End = P;
}
}
//Main.cpp
#include
#include"Find.h"
#include"stack.h"using namespace std;
int M[10][10] ={{1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,0,1,0,1,0,1,1},
{1,0,1,0,1,0,1,0,0,1},
{1,0,1,0,1,0,1,1,0,1},
{1,0,1,0,1,0,1,0,0,1},
{1,0,1,0,1,0,1,0,1,1},
{1,0,1,0,1,0,1,0,1,1},
{1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
int main()
{
Position S,T;
S.x = 1;
S.y = 1;
T.x = 8;
T.y = 1;
Find F(M, S, T);
if (F.AutoFind())
cout << "Success!" << endl;
return 0;
}
最后 【利用自定义栈实现的走迷宫程序_C++】这是看到数据结构书里的题然后写的
我还是个学生,功力有限
望体谅
有更好的代码或者哪里有错就留言吧!
推荐阅读
- SpringBoot调用公共模块的自定义注解失效的解决
- python自定义封装带颜色的logging模块
- 列出所有自定义的function和view
- Spring|Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件
- 程序员客栈TOP收入的萌系开发者心得|程序员客栈TOP收入的萌系开发者心得 - 雨晴
- 自定义MyAdapter
- 【万伽复利】什么是复利(如何利用复利赚钱?)
- Android自定义view实现圆环进度条效果
- C语言进阶栈帧示例详解教程
- 苹果手机如何利用库乐队自制铃声