用栈实现的自动走迷宫
[cpp
sqStack.h
#include "selemtype.h"
#define MaxSize 1000
typedef struct
{
SElemType data[MaxSize];
int top;
}Stack;
Status InitStack(Stack &S);
Status DestroyStack(Stack &S);
Status StackEmpty(Stack &S);
Status Push(Stack &S,SElemType &e);
Status Pop(Stack &S,SElemType &e);
[cpp] view plaincopy
sqStack.cpp
#include "sqstack.h"
#include<iostream>
using namespace std;
Status InitStack(Stack &S)
{
S.top = 0 ;
for( int i = 0 ; i < MaxSize ; i++ )
{
S.data[i].di = 0 ;
S.data[i].ord = 0 ;
S.data[i].seat.c = 0;
S.data[i].seat.r = 0;
}
return true;
}
Status DestroyStack(Stack &S)
{
S.top = 0 ;
return true;
}
Status StackEmpty(Stack &S)
{
bool judger = false ;
if(S.top == 0)
judger = true;
return judger;
}
Status Push(Stack &S,SElemType &e)
{
bool judger = false;
if(S.top < MaxSize)
{
S.data[S.top] = e ;
S.top++;
judger = true ;
}
return judger;
}
Status Pop(Stack &S,SElemType &e)
{
bool judger = false ;
if(S.top > 0)
{
S.top--;
e = S.data[S.top] ;
judger = true ;
}
return judger;
}
[cpp]
Mazepath.h
#define N 15
#define M 22
//分割块占总空间比例
#define V 0.4
typedef struct ElemType
{
int x,y;
char c;
}ElemType;
typedef struct MazeType
{
ElemType arr[N][M];
}MazeType;
Status Pass(MazeType &MyMaze, PosType CurPos);
void FootPrint(MazeType &MyMaze, PosType CurPos);
void MarkPrint(MazeType &MyMaze, PosType CurPos);
PosType NextPos(PosType CurPos, int Dir);
Status MazePath(MazeType &maze, PosType start, PosType end);
[cpp]
Mazepath.cpp
/*------------------------------------------------
走迷宫算法v1.0
N 迷宫行数
M 迷宫列数
V 分割块占总空间比例
ElemType 迷宫元素类型
MazeType 迷宫类型
函数MazePath 走迷宫算法
函数Pass 判断当前位置是否可通过
函数FootPrint 留下足迹
函数MarkPrint 留下不能再走的标记
函数NextPos 计算下一位置
-------------------------------------------------*/
#include "MazePath.h"
Status MazePath(MazeType &maze, PosType start, PosType End)
{
// 算法3.3
// 若迷宫maze中从入口 start到出口 end的通道,则求得一条存放在栈中
// (从栈底到栈顶),并返回TRUE;否则返回FALSE
Stack stack; //定义一个栈
InitStack(stack); //初始化该栈
int count = 0 ; //用来记录通道块的序号
PosType nextPos = start,curPos = start;
SElemType e,pe;
pe.di = 0;
int di = 0 ;
bool judger = true ;
bool uu = true;
while(true)
{
for(int i = 1 ; i <= 4 ; i++ )
{
nextPos = NextPos(curPos,i);
if(Pass(maze,nextPos))
{
count++;
e.seat = curPos ;
e.ord = count;
e.di = i ;
FootPrint(maze,curPos);
Push(stack, e);
curPos = nextPos ;
judger = true ;
if(curPos.c==End.c&&curPos.r==End.r)
{
count++;
e.seat = curPos ;
&
补充:软件开发 , C++ ,