/*======================函数定义===================*/
bool Pass(PosType pos,char (*p)[10]);/*判断当前通道块是否可以通过(该通道块未曾走到过,且可通)*/
void FootPrint(char (*p)[10],PosType pos);/*在数组中pos位置做标记,表示曾经走过*/
PosType NextPos(PosType pos,char direction);/*根据方向确定下一个位置*/
Status MazePath(char (*p)[10],PosType start,PosType end,SqStack *s);/*若迷宫maze中存在从入口start到出口*end的通道,则求得一条存放在栈中,并返回True,否则返回False
*/
int main(){
char Maze[10][10]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};/*迷宫地图,0表示可通*/
SqStack s;
PosType start,end;
start.x=start.y=1;/*设定迷宫入口和出口的坐标*/
end.x=end.y=8;
InitStack(&s);
if(MazePath(Maze,start,end,&s))ShowStack(s,"走出迷宫路径为:");
else printf("找不到迷宫路径");
DestroyStack(&s);
getch();
return 0;
}
bool Pass(PosType pos,char (*p)[10]){
if(pos.x<0 || pos.y<0)return FALSE;/*超出迷宫边界*/
if(pos.x>9 || pos.y>9)return FALSE;
if(p[pos.x][pos.y]==0)return TRUE;/*数组元素为0,表示可通且未曾走过*/
return FALSE;
}
void FootPrint(char (*p)[10],PosType pos){
if(pos.x<0 || pos.x>9 || pos.y<0 || pos.y>9)return;
p[pos.x][pos.y]++;
}
PosType NextPos(PosType pos,char direction){
switch(direction){
case 1:pos.y++;/*朝右*/
break;
case 2:pos.x++;/*朝下*/
break;
case 3:pos.y--;/*朝左*/
break;
case 4:pos.x--;/*向上*/
break;
}
return pos;
}
Status MazePath(char (*p)[10],PosType start,PosType end,SqStack *s){
PosType curpos=start;
SElemType e;
do{
if(Pass(curpos,p)){/*当前位置可以通过*/
FootPrint(p,curpos);/*标志该位置已经走过*/
e.seat=curpos;
e.di=1;/*初始化从右面开始尝试*/
Push(s,e);/*将当前可同路径加入栈*/
if((curpos.x==end.x) && (curpos.y==end.y))return TRUE;/*到达终点*/
curpos=NextPos(curpos,e.di);/*获得下一个尝试路径,继续尝试*/
}
else{
if(!StackEmpty(*s)){
Pop(s,&e);/*此路不通,返回上一通道块*/
while(e.di==4 && !StackEmpty(*s))Pop(s,&e);/*如果通道块其它方向均不通,继续返回上一通道块尝试其它方向*/
if(e.di<4){/*尝试其他方向,可能有可通路径,将该通道块压栈,继续尝试*/
e.di++;
Push(s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!StackEmpty(*s));
return FALSE;
}
其中
/*----------------SqStack.h--*/
/*-------------SqStack.h---
*栈的顺序存储表示
*/
#include "../../include/type.h"
/*===================栈的顺序存储表示====================*/
#define STACK_INIT_SIZE 100/*存储空间初始分配量*/
#define STACKINC 10/*存储空间分配增量*/
typedef struct{
char x;/*通道块的x坐标*/
char y;/*通道块的y坐标*/
}PosType;
typedef struct{
PosType seat;/*通道块在迷宫中的坐标位置*/
char di;/*从此通道块走向下一通道块的“方向"(1,2,3,4)*/
} SElemType;
typedef struct{
SElemType *base;/*在栈构造之前和销毁之后,base的值为null*/
SElemType *top;
int stacksize;/*当前已分配的存储空间,以元素为单位*/
}SqStack;
/*==========================基本操作的函数原型说明===================*/
Status InitStack(SqStack *s);/*构造一个空栈*/
Status DestroyStack(SqStack *s);/*销毁栈S,S不再存在*/
Status ClearStack(SqStack *s);/*将栈置为空栈*/
bool StackEmpty(SqStack s);/*若栈s为空栈,则返回TRUE,否则返回FALSE*/
int StackLength(SqStack s);/*返回栈的长度,即栈s的元素个数*/
Status GetTop(SqStack s,SElemType *e);/*若栈不空,则用e返回s的栈顶元素,并返回OK;否则返回ERR*/
Status Push(SqStack *s,SElemType e);/*将元素e插入为新的栈顶元素*/
Status Pop(SqStack *s,SElemType *e);/*若栈不空,则删除s的栈顶元素,用e返回,并返回OK;否则返回ERR*/
void ShowStack(SqStack s,string info);/*显示栈中的数据*/
用栈实现迷宫
最新推荐文章于 2023-11-05 14:40:49 发布