数据结构-栈/c++/迷宫

1,栈的基本操作和迷宫的数据结构
#include<iostream>
using namespace std;

#define STACK_INIT_SIZE 100
#define STACK_INCREMENT_SIZE 100

typedef struct{
		int x,y;
	}PostType;//坐标结构

typedef struct
{
	int order;//通道快在路径上的序号[1,n]
	PostType seat;//通道快的坐标
	int direction;//方向,1,2,3,4,分别表示东西南北
}Block;//栈元素的类型

typedef Block ElemType;

typedef struct{
	ElemType *base;//栈底指针,总是指向栈底
	ElemType *top;//栈顶指针,总是指向最上元素的上一个位置
	int size;//栈的大小
}SqStack;
//-----基本操作-----

int InitStack(SqStack &S)
{//构造一个空栈
	S.base = new ElemType[100];
	if(!S.base) return 0;
	S.top=S.base;
	S.size=STACK_INIT_SIZE;
	return 1;
}

int StackEmpty(SqStack S)
{//判断S是否为空,为空返回1
	if(S.base==S.top)return 1;
	else return 0;
}

int GetTop(SqStack S,ElemType &e)
{
	if(S.base==S.top) return 0;
	e= *(S.top-1);
	return 1;
}

int Push(SqStack &S,ElemType e)
{
	if(S.top-S.base>=S.size)
	{
		S.base = new ElemType[STACK_INIT_SIZE+STACK_INCREMENT_SIZE];
		if(!S.base) return -1;//堆区分配失败
		S.top=S.base+S.size;
		S.size+=STACK_INCREMENT_SIZE;
	}

	*S.top++ = e;//指针运算符优先级低于自增运算符
	return 1;
}

int Pop(SqStack &S,ElemType &e)
{
	if(S.base==S.top)return -1;
	S.top--;
	e=*S.top;//==e=*--S.top;
}

int ClearStack(SqStack &S)
{
	S.top=S.base;
	return 1;
}

int DestroyStack(SqStack &S)
{
	S.top=NULL;
	S.size=0;
	delete(S.base);
	S.base=NULL;
	return 1;
}

2,迷宫的算法

PostType NextPos(PostType c, int dir)
{//寻找下一个路径块
	PostType direct[5] = {{-1,-1},{0,1}, {1,0}, {0,-1}, {-1,0}}; //{行增量,列增量}
    c.x += direct[dir].x;
    c.y += direct[dir].y;
	return c;
}
int	MazePath(int maze[][8],PostType start,PostType end ,int n)
{//迷宫路径,我们规定:0表示不可通过,1表示可通过,2表示已走路径

	int ans[8][8]={0};
	SqStack S;
	InitStack(S);//先构建空栈
	
	//-----设置各种初值-----
	PostType curpos = start;
	int currstep = 1;//步数,即序号
	Block e;
	do{
		if(maze[curpos.x][curpos.y]==1)
		{//可通过
			cout<<"当前步数:"<<currstep<<endl;
			ans[curpos.x][curpos.y] = currstep;
			maze[curpos.x][curpos.y] = 2;//留下能通过足迹,改变maze中的值

			e.order = currstep;
			e.seat = curpos;
			e.direction = 1;//构造要入栈的元素
			Push(S,e);//入栈
			
			if(curpos.x==end.x && curpos.y==end.y) {
				cout<<"总步数:"<<currstep<<endl;
				for(int i=0;i<8;i++)
				{
					for(int j=0;j<8;j++)
					{
						if(ans[i][j]!=0)cout<<ans[i][j]<<" ";
						else cout<<" ";
					}
					cout<<endl;
				}
						
				return 1;
			}
			curpos = NextPos(curpos,1);//下一个路径块
			currstep++;//步数加1
		}//if
		else //当前不能通过
		{
			if(!StackEmpty(S))
			{
				Pop(S,e);
				while(e.direction==4 && !StackEmpty(S))
				{
					ans[e.seat.x][e.seat.y] = 0;
					maze[e.seat.x][e.seat.y] = 0;//,先弹出栈,在改变状态。留下不能通过的足迹,改变maze中的值
					Pop(S,e);//只要不能通过,都弹出栈,知道找到一个有“可变向的回头路”为止
					--currstep;
				}//while
				if(e.direction<4)
				{//找到一个“可变向的回头路”的点
					e.direction++;//探寻下一个方向
					Push(S,e);//前面将他弹出栈,现在再将他压入栈
					curpos = NextPos(e.seat,e.direction);//下一个路径块
				}//if
			}//if
		}//else
	}while(!StackEmpty(S));
	return 0;
}

3,main,按照此算法,迷宫必须有边界,否则方向判断时数组地址越界
#include<iostream>
#include"Maze.h"
using namespace std;


int main(void)
{
int maze[8][8]={
  {0,0,0,0,0,0,0,0},
   {0,1 ,1 ,1 ,1, 1 ,1,0},
   {0, 1, 1 ,0 ,0 ,1 ,1,0},
  { 0,0, 1, 0 ,0 ,1, 0,0},
  { 0,1, 1, 1 ,0 ,1 ,0,0},
  {0, 1, 1 ,0 ,1, 0 ,0,0},
   { 0,1, 1 ,1, 1 ,1, 1,0},
  {0,0,0,0,0,0,0,0}};
PostType start,end;
start.x=start.y=1;
end.x = end.y = 6;
MazePath(maze,start,end,8);
return 0;
}
//=====算法描述=====
/*
do{
if(当前块可通过)
{
入栈,步数+1,记录路径
if(当前块为结束块) 找到路径,结束


下一块为当前块的东临
}
else
{//不能通过不入栈
if(栈不为空)
{
将最后一个可通过的块从站里取出
while(该块四周都检查完了,或者栈为空)
一直弹出不能通过的块,设置不能通过标志,记录路径
if(该块四周没检查完)
检查其他方向,
入栈,表示该块还是可通过的
}
}
}while(栈不为空)


*/

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值