您的位置:首页 > 编程语言 > C语言/C++

C++ 用栈实现 迷宫求解

2013-03-25 23:20 441 查看
maze.h 源代码

#define  STACK_INIT_SIZE 100
//坐标信息
typedef struct{
int x;
int y;
}PosType;

//栈元素信息
typedef struct{
int ord;//通道块在路径上的序号
PosType seat;//通道块在迷宫中的坐标位置
int di; //从此通道块走向下一通道块的方向
}SElemType;

//判断当前位置是否可通,要求该方块位置不仅是通道块,而且既不在当前路径上,也不是曾经纳入过路径的通道块
bool Pass(PosType pos);
//留下足迹
void FootPrint(PosType pos);
//当前位置的下一位置,东南西北方向分别为1,2,3,4
PosType NextPos(PosType pos,int di);
//留下不能通过足迹
void MarkPrint(PosType pos);

//重载结构体赋值操作


stack.h

#include "maze.h"

typedef struct {
SElemType Qstack[STACK_INIT_SIZE];
int top;
}SqStack;

//构造一个空栈
void InitStack(SqStack &S);
//判栈是否为空
bool StackEmpty(SqStack S);
int StackLength(SqStack S);
void Push(SqStack &S,SElemType e);
void Pop(SqStack &S,SElemType &e);


stack.cpp

#include "stdafx.h"
#include "stack.h"
#include <iostream>
using namespace std;

/************************************************************************/
/* C++中传递参数如果是指针的话,也是可以修改里面的值的,否则不能修改,
除非加上&引用符号                */
/************************************************************************/

//构造一个空栈
void InitStack(SqStack &S){
S.top = -1;
}
//判栈是否为空
bool StackEmpty(SqStack S){
if (S.top == -1)
{
return true;
}
return false;
}
int StackLength(SqStack S){
//S.top -- ;  //这个地方是不能修改栈里面的值的
return S.top +1;
}
void Push(SqStack &S,SElemType e){
if (S.top == STACK_INIT_SIZE -1)
{
cout<<"Add fail , the stack is full "<<endl;
}else{
S.top++;
S.Qstack[S.top] = e;
}
}
void Pop(SqStack &S,SElemType &e){
if(!StackEmpty(S)){
e = S.Qstack[S.top];
S.top --;
}
}


maze.cpp

// maze.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stack.h"

#include <iostream>
using namespace std;

//0表示墙,1表示通道,2表示已走过,3表示不能通过
//其中 maze[1][1] 为起始位置 maze[8][8]
int maze[10][10]={
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
};

int main(int argc, char* argv[])
{
SqStack S;
InitStack(S);
PosType curpos = {1,1};
// 	PosType sss = curpos;   这里结构体可以直接赋值,why?
// 	cout<<sss.x;
int curstep = 1;
do
{
if (Pass(curpos))
{
FootPrint(curpos);
//SElemType e = {curstep,curpos,1}; //不能这么传,难道是没有相应的构造函数?
SElemType e = {curstep,{curpos.x,curpos.y},1};
Push(S,e);
if (curpos.x == 8 && curpos.y == 8)
{
cout<<"success !";
return 8888;
}
curpos = NextPos(curpos,1);
curstep++;
}else{
if (!StackEmpty(S))
{
SElemType e;
Pop(S,e);
while(e.di == 4 && !StackEmpty(S)){
MarkPrint(e.seat);Pop(S,e);
}
if (e.di < 4)
{
e.di++;
Push(S,e);
curpos = NextPos(e.seat,e.di);
}
}
}
} while (!StackEmpty(S));

return 0;
}

//判断当前位置是否可通,要求该方块位置不仅是通道块,而且既不在当前路径上,也不是曾经纳入过路径的通道块
bool Pass(PosType pos){
if (maze[pos.x][pos.y] !=0 && maze[pos.x][pos.y] !=2 && maze[pos.x][pos.y] !=3 )
{
return true;
}
return false;
}
//留下足迹
void FootPrint(PosType pos){
maze[pos.x][pos.y] = 2;
}
//当前位置的下一位置,东南西北方向分别为1,2,3,4
PosType NextPos(PosType pos,int di){
PosType returnPos ={0,0};
if (di == 1)  //由于边缘有0作为墙壁,所以不用考虑数组越界问题
{
returnPos.x = pos.x;
returnPos.y = pos.y +1;
}else if (di == 2)
{
returnPos.x = pos.x +1;
returnPos.y = pos.y;
}else if (di == 3)
{
returnPos.x = pos.x;
returnPos.y = pos.y -1;
}else if (di == 4)
{
returnPos.x = pos.x -1;
returnPos.y = pos.y;
}
return returnPos;
}
//留下不能通过足迹
void MarkPrint(PosType pos){
maze[pos.x][pos.y] = 3;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: