数据结构栈——迷宫寻路,C语言(栈)

记录自己的学习数据结构(C语言版-严蔚敏)的历程
按照书上写的补全,望指教

1.常量声明
constant.h

#define TRUE 1
#define FALSE 2
#define OK 3
#define ERROR 4
#define INFEASIBLE 5
#define OVERFLOW 6

2.栈的基本操作(Get略改动)
Stack.h

#include "constant.h"
#include <stdlib.h>

#define MAXSIZE 100

typedef struct
{
	int x;
	int y;
}PosType;

typedef struct
{
	int ord;
	PosType seat;
	int di;
}Box;

typedef int Status;
typedef Box ElemType;

// 结构体定义,base为栈底指针,base为NULL时表示栈不存在,top为栈顶指针,指向栈顶元素的下一个位置!!!
//top == base 表示空栈 
typedef struct 
{
	ElemType *base;
	ElemType *top;
	int stacksize;
}SqStack;

// 顺序栈的初始化 
Status InitStack(SqStack &S)
{
	S.top = S.base = new ElemType[MAXSIZE];
	if(!S.base) exit(ERROR);
	S.stacksize = MAXSIZE;
	return OK;
}

// 顺序栈的入栈
// S是一个顺序栈,e是要入栈的元素 
Status Push(SqStack &S, ElemType e)
{
	//如果栈满,返回ERROR 
	if(S.top-S.base == S.stacksize) exit(ERROR);
	//后置加,即*S.top=e; S.top++;  
	*S.top++ = e;
	return OK;
} 

// 顺序栈的出栈
// S是一个顺序栈,出栈元素用e带回 
Status Pop(SqStack &S, ElemType &e)
{
	//如果栈空,返回ERROR 
	if(S.top == S.base) exit(ERROR);
	//前置减,即e=*S.top; S.top--;  
	e = *--S.top;
	return OK;
} 

// 取栈顶元素
// S是一个顺序栈,栈顶元素用e带回 
ElemType Get(SqStack &S, ElemType &e)
{
	//如果栈空,返回ERROR 
	if(S.top == S.base) exit(ERROR);
	//区别于出栈,只是取栈顶元素所以top不能变,但top是指向栈顶元素的下一个位置!所以用*(S.top - 1)表示栈顶元素 
	e = *(S.top - 1); 
	return e ; 
} 

main.cpp主要实现
用二维数组创建地图,0代表可以通过,1代表不能通过。

#include <iostream>
#include "Stack.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

//函数申明 
Status Pass(PosType now);	//判断当前块是否能通过 
void FootPrint(PosType now);//留下已走过标记 
void MarkPrint(PosType now);//留下不可通过标记 
PosType NextPos(PosType now,int di);//到下一位置 
Status MazePath(PosType start, PosType end);//寻找一条可能路径 

//初始化地图(可自行修改,方便测试)
int maze[6][6] ={	{0,1,0,0,0,1},
					{1,1,0,1,0,0},
					{0,0,1,1,1,0},
					{1,0,0,0,0,1},
					{1,1,1,1,1,1},
					{1,0,0,0,0,0}
			 	}; 
			 	
int main(int argc, char** argv) {
	//定义入口和出口,结构体声明见Stack.h 
	PosType start, end;
	start.x = 0; start.y = 0;
	end.x = 5; end.y = 5;
	//找路径,有就输出,没有输出语句提示
	MazePath(start,end);
	return 0;
}



Status MazePath(PosType start, PosType end)
{	
	SqStack S;
	InitStack(S);
	//curpos是当前位置,初值是start
	PosType curpos = start;
	//curstep记录步数,入口算一步(我脑子不好使)
	int curstep = 1;
	Box e;
	do
	{
		//如果curpos可以通过 
		if(Pass(curpos) == OK) 
		{
			//留下已通过标记 
			FootPrint(curpos);
			//用e记录当前路径元素 
			e.ord = curstep;
			e.seat = curpos;
			e.di = 1;
			//将e入栈S 
			Push(S,e);
			//如果到达终点 
			if(curpos.x == end.x && curpos.y == end.y)
			{
				printf("可以找到一条可能路径\n");
				while(S.base!=S.top)
				{
					//这里可以通过判断di来输出方向,根据curstep输出步数,略去
					printf("(%d,%d)\t",S.base->seat.x,S.base->seat.y);
					S.base++;
				}
				return 0; 
			}
			curpos = NextPos(curpos,1);
			curstep++;
		} 
		//如果不能通过 
		else
		{
			//如果栈不空 
			if(S.base != S.top)
			{
				//e出栈S 
				Pop(S,e);
				//当4个方向都探索完,且栈不空 
				while((e.di == 4) && (S.base != S.top))
				{
					//留下不可通过标记 
					MarkPrint(curpos);
					//e出栈S 
					Pop(S,e);
					//很坑的点,在上一个语句出栈后可能为空栈,
					//取不到栈顶元素(空栈)时也应该判定为不能找到路。 
					if(S.base != S.top)
					{
						//取栈顶元素
						curpos = Get(S,e).seat;
					}
					else
					{
						printf("找不到一条可能路径\n");
						return 0;	
					} 
				}
				//如果还有其他方向未探索 
				if(e.di < 4) 
				{
					//记录探索方向 
					e.di++; 
					//e入栈S
					Push(S,e);
					curpos = NextPos(curpos,e.di); 
				} 
			}
		}
	}while(S.base != S.top);//栈不空,继续寻找
	printf("找不到一条可能路径\n");
	return 0; 
}

void FootPrint(PosType now)
{
	maze[now.x][now.y] = 1;
}

PosType NextPos(PosType now,int di)
{
	switch(di)
	{
		case 1: 
		{
			now.y++; 
			break;
		}
		case 2:
		{
			//1方向不通,但是curpos.y在1时++了,所以先得--退回上一个路径,下面同理
			now.y--;
			now.x++; 
			break;
		}
		case 3:
		{
			now.x--;
			now.y--; 
			break;
		}
		case 4:
		{
			now.y++;
			now.x--; 
			break;
		}
	}
	return now;
}

Status Pass(PosType now)
{
	//对应点可通且不越界
	if(maze[now.x][now.y] == 0 && now.x >= 0 && now.x < 6 && now.y >= 0 && now.y < 6)
		return OK;
	else
		return ERROR; 
}

void MarkPrint(PosType now)
{
	maze[now.x][now.y] = 1;
}
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include using std::cout; using std::cin; #include #include #include #define OVERFLOW -2 #define INIT_SIZE 100 //存储空间初始分配量 #define INCREMENT 10 //存储空间分配增量 typedef struct{ int r; int c; }PostType;//迷宫中r行c列的位置 typedef struct{ int ord; //当前位置在路径上的序号 PostType seat;//当前坐标 int di; //往下一坐标的方向 }SElemType; //元素类型 typedef struct{ SElemType* base;//基址,构造前销毁后为空 SElemType* top;//顶 int stackSize; //容量 }Stack; //类型 int InitStack(Stack &S){ //构造空s S.base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType)); if(!S.base) exit(OVERFLOW);//存储分配失败 S.top=S.base; S.stackSize=INIT_SIZE; return 0; } int StackEmpty(Stack S)//若s为空返回TRUE,否则返回FALSE { if(S.top==S.base) return 1; return 0; } int Push(Stack &S,SElemType e){ //插入元素e为新的顶元素 if(S.top-S.base >=S.stackSize){//满,加空间 S.base=(SElemType *)realloc(S.base,(S.stackSize+INCREMENT)*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); //存储分配失败 S.top=S.base+S.stackSize; S.stackSize+=INCREMENT; } *S.top++=e; return 1; } int Pop(Stack &S,SElemType &e){//若不空删除,顶元素用e返回 if(S.top==S.base) return 0; e=*--S.top; return 1; } int DestroyStack(Stack &S){//销毁S, free(S.base); S.top=S.base; return 1; } #define MAXLEN 16//迷宫包括外墙最大行列数目 typedef struct{ int r; int c; char adr[MAXLEN][MAXLEN]; }MazeType; //迷宫类型 int InitMaze(MazeType &maze){ //初始化迷宫若成功返回TRUE,否则返回FALSE int i,j; cout<>maze.r>>maze.c; //迷宫行和列数 for(i=0;i<=maze.c+1;i++){//迷宫行外墙 maze.adr[0][i]='#'; maze.adr[maze.r+1][i]='#'; } for(i=0;i<=maze.r+1;i++){//迷宫列外墙 maze.adr[i][0]='#'; maze.adr[i][maze.c+1]='#'; } for(i=1;i<=maze.r;i++) for(j=1;j<=maze.c;j++) maze.adr[i][j]=' ';//初始化迷宫 int m=1,n=maze.c; for(m;m1;n--) maze.adr[6][n]='#'; maze.adr[4][5]='#'; maze.adr[4][6]='#'; maze.adr[3][3]='#'; maze.adr[3][2]='#'; maze.adr[5][7]='#'; maze.adr[5][6]='#'; maze.adr[8][3]='#'; maze.adr[7][2]='#'; maze.adr[7][5]='#'; return 1; }//InitMaze int Pass(MazeType maze,PostType curpos){ if(maze.adr[curpos.r][curpos.c]==' ') return 1; else return 0; }//Pass int FootPrint(MazeType &maze,PostType curpos){ //若走过并且可通返回TRUE,否则返回FALSE //在返回之前销毁S maze.adr[curpos.r][curpos.c]='!';//"*"表示可通 return 1; }//FootPrint PostType NextPos(PostType &curpos,int i){ //指示并返回下一位置的坐标 PostType cpos; cpos=curpos; switch(i){ case 1 : cpos.c+=1; break; case 2 : cpos.r+=1; break; case 3 : cpos.c-=1; break; case 4 : cpos.r-=1; break; default: exit(0); } return cpos; }//Nextpos int MarkPrint(MazeType &maze,PostType curpos){ maze.adr[curpos.r][curpos.c]='@';//"@"表示曾走过但不通 return 1; } int MazePath(MazeType &maze,PostType start,PostType end){ //若迷宫maze存在从入口start到end的通道则求得一条存放在中 Stack S; PostType curpos; int curstep;//当前序号,1.2.3.4分别表示东,南,西,北方向 SElemType e; InitStack(S); curpos=start; //设置"当前位置"为"入口位置" curstep=1; //探索第一步 do{ if(Pass(maze,curpos)){//当前位置可以通过,即是未曾走到过的通道 FootPrint(maze,curpos);//留下足迹 e.ord=curstep; e.seat=curpos; e.di=1; Push(S,e); //加入路径 if(curpos.r==end.r&& curpos.c==end.c) if(!DestroyStack(S))//销毁失败 exit(OVERFLOW); else return 1; //到达出口 else{ curpos=NextPos(curpos,1); //下一位置是当前位置的东邻 curstep++; //探索下一步 } } else{ //当前位置不通 if(!StackEmpty(S)){ Pop(S,e); while(e.di==4 && !StackEmpty(S)){ MarkPrint(maze,e.seat); Pop(S,e); //留下不能通过的标记,并退一步 } if(e.di < 4){ e.di++;//换下一个方向探索 Push(S,e); curpos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻位置 } } } }while(!StackEmpty(S)); if(!DestroyStack(S))//销毁失败 exit(OVERFLOW); else return 0; }//MazePath void PrintMaze(MazeType &maze){ //将标记路径信息的迷宫输出 int i,j; cout<<"\n——!为所求迷宫路线路线——:\n\n"; cout<<" "; for(i=0;i<=maze.r+1;i++)//打印列数名 cout<<" "<<i; cout<<"\n\n"; for(i=0;i<=maze.r+1;i++){ cout<<" "<<i;//打印行名 for(j=0;j<=maze.c+1;j++) cout<<" "<<maze.adr[i][j];//输出迷宫路径 cout<<"\n\n"; } } void main(){ MazeType maze; PostType start,end; char cmd; do{ cout<<"-------建立迷宫--------\n"; if(!InitMaze(maze)){ cout<<"\n——建立有误——!!!\n"; exit(OVERFLOW); } do{ cout<>start.r>>start.c; if(start.r>maze.r || start.c>maze.c){ cout<maze.r || start.c>maze.c); do{ cout<>end.r>>end.c; if(end.r>maze.r || end.c>maze.c){ cout<maze.r || end.c>maze.c); if(!MazePath(maze,start,end)) cout<<"\n不能求得路径!\n"; else PrintMaze(maze); cout<>cmd; }while(cmd=='y' || cmd=='Y'); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值