资源描述:
《C语言数据结构迷宫问题》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#defineM20#defineN20#definevisited2#defineTRUE1#defineFALSE0#defineINITSIZE100typedefintStatus;typedefstruct{//坐标点结构体inty;//每个可通的行坐标intx;//每个可通的列坐标}PosType;typedefstruct{intord;//通道块在路径上的“序号”intdi;//从此通道块走向下一通道块的“方向”PosTypeseat;//通道块在迷宫中的“坐标位置
2、”}MazeNode;//迷宫节点typedefstruct{MazeNodebase[INITSIZE];inttop;//栈顶指针}Stack;typedefstruct{//用于存储迷宫的路径PosTypecoor[INITSIZE];inttop;}Postion;voidRandMatrix();//随机生成迷宫intInitStack(Stack*);//初始化栈intInitStack1(Postion*);intStackEmpty(Stack*);//判断栈是否为空intStackEmpty1(Postion*);intStackIsFull(Stack*);//
3、判断栈是否满了intStackIsFull1(Postion*);//判断栈是否满了intPush(Stack*s,MazeNodem);//压栈intPush1(Postion*,PosType);intPop(Stack*s,MazeNode*m);//出栈intPop1(Postion*,PosType*);intDestroyStack(Stack*s);//撤销栈intPass(PosTypepos);//判断指定坐标是否可通过intFootPrint(PosTypepos);//标记能通过的PosTypeNextCoord(PosTypepos,inti);//获取下一
4、位置intMarkPrint(PosTypepos);//留下不能通过的标记,并退回一步intMazePath(PosTypestart,PosTypeend,Postion*);//从迷宫的入口到出口查找voidPrintMaze(Postion*);//输出迷宫intmg[M][N];//生成一个M*N的迷宫intmain(){inth=1;PosTypestart,end;PostionP;while(h){printf("创建迷宫");InitStack1(&P);RandMatrix();printf("");printf("1、重新生成迷宫,0、就这个:");s
5、canf("%d",&h);}do//输入迷宫入口坐标{printf("输入迷宫入口坐标");scanf("%d%d",&start.x,&start.y);if(start.x>N
6、
7、start.y>M){printf("输入的坐标越界,请重新输入!");continue;}}while(start.x>N
8、
9、start.y>M);do//输入迷宫出口坐标{printf("输入迷宫出口坐标:");scanf("%d%d",&end.x,&end.y);if(end.x>N
10、
11、end.y>M){printf("输入的坐标越界,请重新输入!");continue;}}
12、while(end.x>N
13、
14、end.y>M);if(!MazePath(start,end,&P))//调用函数查找路径{printf("无法通过!");}else{PrintMaze(&P);//打印找到的路径}return0;}intInitStack(Stack*s){s->top=-1;return1;}intInitStack1(Postion*s){s->top=-1;return1;}intStackEmpty(Stack*s){if(s->top==-1)return1;return0;}intStackEmpty1(Postion*s){if(s->to
15、p==-1)return1;return0;}intStackIsFull(Stack*s){if(s->top==INITSIZE-1)return1;elsereturn0;}intStackIsFull1(Postion*s){if(s->top==INITSIZE-1)return1;elsereturn0;}intPush(Stack*s,MazeNodem){if(!StackIsFull(s)){s->top++;s->base[s->top]=m;}r