C++ 用栈实现 迷宫求解

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;
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值