栈的应用-迷宫问题-数据结构(C语言版)-源代码(直接运行)
C语言实验:迷宫问题(搜索,C语言实现栈、队列)

C语⾔实验:迷宫问题(搜索,C语⾔实现栈、队列)Description给定迷宫起点和终点,寻找⼀条从起点到终点的路径。
(0,1)(2,0)起点(1,1)(1,2)(1,3)(1,4)(2,0)(2,1)(2,4)(3,0)(3,1)(3,2)终点(3,4)(4,1)上图中黄⾊代表墙,⽩⾊代表通路,起点为(1,1),终点为(3,4)。
要求搜寻策略是从起点开始按照“上、下、左、右”四个⽅向寻找终点,到下⼀个点继续按照“上、下、左、右”四个⽅⾯寻找,当该结点四个⽅向都搜寻完,但还没到终点时,退回到上⼀个点,直到找到终点或者没有路径。
⽐如上图从(1,1)开始,向上(0,1)不通,向下到(2,1);到了(2,1)后继续按“上、下、左、右”四个⽅⾯寻找,上已经⾛过,向下到(3,1);到(3,1)后上已经⾛过,下和左不通,向右到(3,2);到(3,2)四个⽅⾯都不通,回到(3,1)四个⽅向都不通,再回到(2,1),(1,1);到达(1,1)后下已经⾛过,左不通,继续向右⾛,重复这个过程最后到达(3,4)。
Input第⼀⾏两个数m和n表⽰迷宫的⾏数和列数。
迷宫⼤⼩不超过100×100第⼆⾏四个数x1,y1,x2,y2分别表⽰起点和终点的坐标。
接下来是m⾏n列的数,⽤来表⽰迷宫,1表⽰墙,0表⽰通路。
Output从起点到终点所经过的路径的坐标。
如果不存在这样的路径则输出“No Path!”。
Sample Input5 61 1 3 41 1 1 1 1 11 0 0 0 0 11 0 1 1 0 11 0 0 1 0 11 1 1 1 1 1Sample Output(1 1)(1 2)(1 3)(1 4)(2 4)(3 4)1.思路:(1)若当前点是终点,dfs函数返回1;(2)若不是终点,将此点标记为1,对该点4个⽅向进⾏搜索,实现⽅式为定义int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 通过⼀个⼩循环: for(int i = 0; i < 4; i++) { position nextp; nextp.x = dir[i][0] + now.x;nextp.y = dir[i][1] + now.y;...... } 进⾏搜索;若该点的下⼀个点nextp不是墙,未⾛,并且没有超界则将nextp压⼊栈中,递归调⽤dfs,若此过程经过(1)判断返回了1,说明最终找到了通往终点的路,便可以返回1,结束函数,此时栈中已储存了通往终点的路径,若没有通路,则弹出栈顶元素,根据递归原理该路径上的所有点都会弹出并标记未⾛,回溯到之前的点,继续向其他⽅向搜索,直到找到终点或遍历完整个图。
数据结构栈的应用(迷宫求解)

栈的应用迷宫求解任务:可以输入一个任意大小的迷宫数据,用非递归的方法求出一条走出迷宫的路径,并将路径输出;源代码:#include<stdio.h>#include<stdlib.h>/*数据定义*/typedefenum { ERROR, OK } Status;typedefstruct{int row; //row表示"行"号int line; //line表示"列"号}PosType; //位置的元素类型typedefstruct{intord; //该通道在路径上的"序号"PosType seat; //通道块在迷宫中的"坐标位置"int di; //从此通道走向下以通道块的"方向"}SElemType; //栈的元素类型typedefstruct{SElemType * base;SElemType * top;intstacksize;}SqStack;/*函数原型说明*/Status InitStack(SqStack&S); //创建一个空栈SStatus Push(SqStack&S,SElemType&a); //插入新元素aStatus Pop(SqStack&S,SElemType&a); //删除栈顶元素,a返回其值Status StackEmpty(SqStack S); //检查是否空栈Status MazePath(int maze[12][12],SqStack&S, PosType start, PosType end); //找通路void Initmaze(int maze[12][12],intx,int y); //初始化迷宫void printmaze(int maze[12][12],intx,int y); //显示迷宫Status Pass(int maze[12][12],PosTypeCurPos); //判断当前位置是否可通void Markfoot(int maze[12][12], PosTypeCurPos); //标记当前位置不可通PosTypeNextPos(PosTy peCurPos, intDir); //进入下一位置void printpath(int maze[12][12],SqStackS,intx,inty,PosTypestart,PosType end); //显示通路/*主函数*/void main (void){PosTypestart,end;int t=0;SqStack S;int maze[12][12];do{if(t!=0)printf("\n");for(int i=0;i<20;i++){printf("* ");}if(t==0){printf("\n*\t欢迎来到迷宫世界\t * \n");printf("*\t 1.设置迷宫\t\t * \n");}else{printf("\n* 请继续选择:\t\t\t * \n");printf("*\t 1.设置迷宫\t\t * \n");}printf("*\t 2.设置迷宫路径\t\t * \n");printf("*\t 3.输出迷宫通路路径\t * \n");printf("*\t 4.结束\t\t\t *\n");t++;for(int j=0;j<20;j++){printf("* ");}printf("\n");int s;printf("请选择:");scanf("%d",&s);intaa;intx,y;switch(s){case 1://1.初始化迷宫aa=1;printf("迷宫设置:请输入\n"); //设置迷宫大小do{printf("行X(x<10)=");scanf("%d",&x);printf("列Y(y<10)=");scanf("%d",&y);if(x<1 || x>10||y<1 || y>10){printf("输入错误!\n");}}while(x<1 || x>10||y<1 || y>10);Initmaze(maze,x,y); //初始化迷宫printmaze(maze,x,y); //显示所创建的迷宫break;case 2://寻找迷宫路径if(aa==1){aa=2; //设置入口和出口do{printf("输入入口行坐标:");scanf("%d",&start.row);if(start.row>x)printf("输入错误!\n");}while(start.row>x);do{printf("输入入口列坐标:");scanf("%d",&start.line);if(start.line>y)printf("输入错误!\n");}while(start.line>y);do{printf("输入出口行坐标:");scanf("%d",&end.row);if(end.row>x)printf("输入错误!\n");}while(end.row>x);do{printf("输入出口列坐标:");scanf("%d",&end.line);if(end.line>y)printf("输入错误!\n");}while(end.line>y);}elseprintf("请先初始化迷宫!\n");printf("\n所设置入口坐标为(%d,%d),出口坐标为(%d,%d).\n",start.row,start.line,end.row,end.line);break;case 3://输出此迷宫通路路径if(aa==2){aa=3;if(MazePath(maze,S,start,end)) //若有通路,显示通路printpath(maze,S,x,y,start,end);elseprintf("\n抱歉,找不到通路!\n\n");elseprintf("请先初始化迷宫并寻找路径!\n");break;case 4:exit(0);break;default:exit(0);}}while(1);}/*若迷宫maze中从入口start到出口end的通道,则求得一条存放在栈中*/Status MazePath(int maze[12][12],SqStack&S, PosType start, PosType end){PosTypecurpos;intcurstep;SElemType e;InitStack(S);curpos = start; // 设定"当前位置"为"入口位置curstep = 1; // 探索第一步do {if (Pass(maze,curpos)) // 当前位置可通过,即是未曾走到过的通道块{Markfoot(maze,curpos); // 留下足迹e.di =1;e.ord = curstep;e.seat= curpos;Push(S,e); // 加入路径if (curpos.row==end.row&&curpos.line==end.line)return OK; // 到达终点(出口)curpos = NextPos(curpos, 1); // 下一位置是当前位置的东邻curstep++; // 探索下一步}else // 当前位置不能通过{if (!StackEmpty(S)){Pop(S,e);while (e.di==4 && !StackEmpty(S)){Markfoot(maze,e.seat); // 留下不能通过的标记,并退回一步Pop(S,e);}if (e.di<4)e.di++; // 换下一个方向探索Push(S, e);curpos = NextPos(e.seat, e.di); // 当前位置设为新方向的相邻块}}}} while (!StackEmpty(S));return ERROR;}/*初始化迷宫时间复杂度:n^2*/voidInitmaze(int maze[12][12],intx,int y){char select;do{printf("创建方式A:自动生成B:手动创建\n");label: s canf("%c",&select);if(select=='a'||select=='A') //自动生成{for(int i=1;i<x+1;i++){for(int j=1;j<y+1;j++)maze[i][j]=rand()%2;}break;}else if(select=='b'||select=='B') //手动设置{printf("按行输入%d*%d数据,0代表可通,1代表不可通(每行以Enter结束):\n",x,y);for(int i=1;i<x+1;i++){for(int j=1;j<y+1;j++)scanf("%d",&maze[i][j]);maze[i][y+1]=1;}for(i=0;i<x+2;i++)maze[x+1][i]=1;break;}else if(select=='\n')goto label; //排除Enter键的影响elseif(select!='b'||select!='B'||select!='a'||select!='A')printf("输入错误!\n");}while(select!='b'||select!='B'||select!='a'||select!='A');}/*显示迷宫时间复杂度:n^2*/voidprintmaze(int maze[12][12],intx,int y){printf("\n\n");printf("显示所建的迷宫(#表示墙):\n");printf("%c ",'#');printf("%c ",'y');for(int j=1;j<y+1;j++)printf("%d ",j);printf("%c ",'#');printf("\nx ");for(int i=1;i<y+3;i++)printf("%c ",'#');printf("\n");for(i=1;i<x+1;i++){if(i<x+1)printf("%d ",i);elseprintf("%c ",'#');printf("%c ",'#');for(int j=1;j<y+1;j++){printf("%d ",maze[i][j]);}printf("%c",'#');printf("\n");}for(i=0;i<y+3;i++)printf("%c ",'#');printf("\n");}/*输出路径时间复杂度:n^2*/voidprintpath(int maze[12][12],SqStackS,intx,inty,PosTypestart,PosType end) {printf("\n\n\001通路路径:\n");SElemType * p=S.base;while(p!=S.top){maze[p->seat.row][p->seat.line]=2; //标记为路径中的点p++;}printf("\001路径图为:\n");printf("%c ",'#');printf("%c ",'y');for(int j=1;j<y+1;j++)printf("%d ",j);printf("%c ",'#');printf("\nx ");for(int m=1;m<y+3;m++)printf("%c ",'#');printf("\n");for(int i=1;i<x+1;i++){if(i<x+1)printf("%d ",i);elseprintf("%c ",'#');printf("%c ",'#');for(int j=1;j<y+1;j++){if(maze[i][j]==2){if(i==start.row&&j==start.line||i==end.row&&j==end.line){if(i==start.row&&j==start.line)printf("i ");if(i==end.row&&j==end.line)printf("o ");}elseprintf("%c ",' ');}elseprintf("%c ",'#');}printf("%c ",'#');printf("\n");}for(i=0;i<y+3;i++)printf("%c ",'#');printf("\n\n");printf("\001路径线路为:\n");SqStack M;InitStack(M);SElemType a;while (!StackEmpty(S)){Pop(S,a);Push(M,a);}while (!StackEmpty(M)){Pop(M,a);printf("(%d,%d)",a.seat.row,a.seat.line);}printf("\n");printf("路径输出成功!\n");}/* 判断当前位置是否可通*/Status Pass(int maze[12][12],PosTypeCurPos){if(maze[CurPos.row][CurPos.line]==0)return OK; // 如果当前位置是可以通过,返回1 elsereturn ERROR; // 其它情况返回0}/* 标记当前位置不可通*/voidMarkfoot(int maze[12][12],PosTypeCurPos){maze[CurPos.row][CurPos.line]=1;}/*进入下一位置*/PosTypeNextPos(PosTy peCurPos, intDir){PosTypeReturnPos;switch (Dir){case 1:ReturnPos.row=CurPos.row;ReturnPos.line=CurPos.line+1;break;case 2:ReturnPos.row=CurPos.row+1;ReturnPos.line=CurPos.line;break;case 3:ReturnPos.row=CurPos.row;ReturnPos.line=CurPos.line-1;break;case 4:ReturnPos.row=CurPos.row-1;ReturnPos.line=CurPos.line;break;}returnReturnPos;}/*创建一个空栈S*/Status InitStack(SqStack&S){S.base=(SElemType *)malloc(100*sizeof(SElemType));if(!S.base)return ERROR;S.top=S.base;S.stacksize=100;return OK;}/*插入新元素a*/Status Push(SqStack&S,SElemType&a){*S.top++=a;return OK;}/* 删除栈顶元素,a返回其值*/Status Pop(SqStack&S,SElemType&a){if(S.top==S.base)return ERROR;a=*--S.top;return OK;}/*检查是否空栈*/Status StackEmpty(SqStack S){if(S.top==S.base)return OK;return ERROR;}。
数据结构迷宫问题源代码

#include<stdio.h>#include<stdlib.h>#include"Status.h"typedef struct{int x;int y;}posType;typedef struct{posType pos;int dirc;}SElemType;#include"SqStack.h"#define Row 12#define Col 12posType nextPos(posType curPos,int d){posType pos;switch(d){case 1: pos.x=curPos.x;pos.y=curPos.y+1;break;case 2: pos.x=curPos.x+1;pos.y=curPos.y;break;case 3: pos.x=curPos.x;pos.y=curPos.y-1;break;case 4: pos.x=curPos.x-1;pos.y=curPos.y;break;}return pos;}Status canGo(posType pos,int M[Row][Col]){if(M[pos.x][pos.y]==0) return TRUE;else return FALSE;}Status Maze(int M[Row][Col],posType start,posType end){ SqStack S;SElemType e;posType curPos;int d,step;InitSqStack(&S,100);curPos=start;d=4;do{if(canGo(curPos,M)){M[curPos.x][curPos.y]=2;e.pos=curPos;e.dirc=d;Push(&S,e);if(curPos.x==end.x&&curPos.y==end.y) break;d=1;curPos=nextPos(curPos,d);}else if(d<4){d++;getTop(S,&e);curPos=nextPos(e.pos,d);}else if(!stackIsEmpty(S)){Pop(&S,&e);d=e.dirc;curPos=e.pos;}}while(!stackIsEmpty(S));if(!stackIsEmpty(S)){Pop(&S,&e);M[e.pos.x][e.pos.y]='e';d=e.dirc;for(step=stackLength(S);step>0;step--){Pop(&S,&e);switch(d){case 1: M[e.pos.x][e.pos.y]=26;break;case 2: M[e.pos.x][e.pos.y]=25;break;case 3: M[e.pos.x][e.pos.y]=27;break;case 4: M[e.pos.x][e.pos.y]=24;break;}d=e.dirc;}M[start.x][start.y]='s';return TRUE;}else return FALSE;}void printMaze(int M[Row][Col],posType start,posType end){int i,j;printf("迷宫:入口(%d,%d),出口(%d,%d)\n",start.x,start.y,end.x,end.y); printf("\t%3c",' ');for(i=0;i<Col;i++)printf("%2d ",i);printf("\n");for(i=0;i<Row;i++){printf("\t%2d",i);for(j=0;j<Col;j++){if(M[i][j]==1)printf("|||");else if(M[i][j]==0)printf(" ");else if(M[i][j]==2)printf(" = ");else printf(" %c",M[i][j]);}printf("\n");}printf("\n");}int main(){int M[Row][Col]={{1,1,1,1,1,1,1,1,1,1,1,1},{1,0,0,0,1,1,0,1,1,1,0,1},{1,0,1,0,0,1,0,0,1,0,0,1},{1,0,1,1,0,0,0,1,1,0,1,1},{1,0,0,1,0,1,0,0,0,0,0,1},{1,1,0,1,0,1,1,0,1,0,1,1},{1,0,0,1,0,0,0,0,1,1,1,1},{1,0,1,0,1,1,1,1,0,0,1,1},{1,0,0,0,0,1,1,0,0,0,1,1},{1,0,1,0,1,0,0,0,1,0,0,1},{1,0,0,0,0,0,1,0,0,1,0,1},{1,1,1,1,1,1,1,1,1,1,1,1} };posType start,end;start.x=1;start.y=1;end.x=10;end.y=10;printMaze(M,start,end);if(Maze(M,start,end)){printf("找到可行路径:\n");printMaze(M,start,end);}else printf("无可行路径!\n");system("pause");return 0;}。
数据结构(C语言版)实验报告(迷宫)

《数据结构与算法》实验报告评分依据及结果一、需求分析1.问题描述:以一个m*n的长方阵表示迷宫,空格和感叹号分别表示迷宫中的通路和障碍。
设计一个程序,对随机产生的迷宫,求一条从入口到出口的通路,或得出没有通路的结论。
2.基本要求首先实现一个以链表为存储结构的栈类型,然后编写一个求解迷宫的非递归程序。
求得的通路以三元组(i,j,d)的形式输出。
其中(i,j)表示迷宫的一个坐标,d表示走到下一座标的方向。
3.程序的执行命令有:1)输入迷宫的列数2)输入迷宫的行数二、概要设计为实现上述功能,需要以一个链表为存储结构的栈类型1.栈的顺序存储表示typedef struct{int x; /*列*/int y; /*行*/}PosType; //坐标位置类型typedef struct{int ord; //通道块在路径上的"序号"PosType seat; //通道块在迷宫中的"坐标位置"int di; //从此通道块走向下一通道块的"方向"}SElemType; //栈的元素类型typedef struct{SElemType *base;SElemType *top;int stacksize; //当前已分配的存储空间,以元素为单位}SqStack;//迷宫程序typedef struct{int lie; /*列数*/int hang; /*行数*/char a[999][999];}MazeType; /*迷宫类型*/2.基本操作int InitStack(SqStack *S)//分配空间int GetTop(SqStack *S,SElemType *e) //若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERRORint Push(SqStack *S,SElemType *e)//插入元素e作为新的栈顶元素int Pop(SqStack *S,SElemType *e) //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERRORint generatemaze( MazeType *maze)// 随机生成迷宫int Pass(MazeType *maze, PosType curpos ) //判断当前位置可否通过int FootPrint(MazeType *maze,PosType curpos) //留下足迹int MarkPrint(MazeType *maze,PosType curpos) //留下不能通过的标记PosType NextPos(PosType curpos,int di) //返回当前位置的下一位置int MazePath(MazeType *maze,PosType start,PosType end) //若迷宫有解,则求得一条存放在栈中(从栈底到栈顶),并返回OK,否则返回ERROR void PrintMaze(MazeType *maze) //打印迷宫三、详细设计//程序的头文件#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <string.h>#include <time.h>//函数的返回值#define OK 1#define ERROR 0#define NULL 0#define OVERFLOW -2#define STACK_INIT_SIZE 100#define STACKINCREMENT 10//栈的顺序存储表示typedef struct{int x; /*列*/int y; /*行*/}PosType; //坐标位置类型typedef struct{int ord; //通道块在路径上的"序号"PosType seat; //通道块在迷宫中的"坐标位置"int di; //从此通道块走向下一通道块的"方向"}SElemType; //栈的元素类型typedef struct{SElemType *base;SElemType *top;int stacksize; //当前已分配的存储空间,以元素为单位}SqStack;//基本操作int InitStack(SqStack *S){S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));if(!S->base)exit(OVERFLOW);S->top=S->base;S->stacksize=STACK_INIT_SIZE;return OK;}//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERRORint GetTop(SqStack *S,SElemType *e){if(S->top==S->base)return ERROR;*e=*(S->top-1);return OK;}int Push(SqStack *S,SElemType *e)//插入元素e作为新的栈顶元素{if(S->top-S->base>=S->stacksize)/*栈满,追加存储空间*/{S->base=(SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));if(!S->base)exit(OVERFLOW);S->top=S->base+S->stacksize;S->stacksize+=STACKINCREMENT;}*S->top++=*e;return OK;}//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERRORint Pop(SqStack *S,SElemType *e){if(S->top==S->base)return ERROR;*e=*--S->top;return OK;}int StackEmpty(SqStack *S){return(S->top==S->base);}//迷宫程序typedef struct{int lie; /*列数*/int hang; /*行数*/char a[999][999];}MazeType; /*迷宫类型*//*随机生成迷宫*/int generatemaze( MazeType *maze){int i,j;maze->a[0][0]=2;maze->a[++maze->hang][++maze->lie]=3; /*设置外墙*/maze->a[0][maze->lie]='!';maze->a[maze->hang][0]='!';for(j=1;j<maze->lie;j++){maze->a[0][j]='!';maze->a[maze->hang][j]='!';}for(i=1;i<maze->hang;i++){maze->a[i][0]='!';maze->a[i][maze->lie]='!';}srand((unsigned)time( NULL ));rand();for(i=1; i <maze->hang; i++)for(j=1;j<maze->lie;j++){if (rand()>=RAND_MAX/4) maze->a[i][j] =' '; //' ' 暗示出路else maze->a[i][j] ='!'; //'!'暗示无出路}return OK;}int Pass(MazeType *maze, PosType curpos ) //判断当前位置可否通过{if ((curpos.x < 1) || (curpos.x >= maze->lie))return ERROR;if ((curpos.y < 1) || (curpos.y >= maze->hang))return ERROR;if (maze->a[curpos.y][curpos.x]==' ')return OK;else return ERROR;}int FootPrint(MazeType *maze,PosType curpos) //留下足迹{maze->a[curpos.y][curpos.x]='*';return OK;}int MarkPrint(MazeType *maze,PosType curpos) //留下不能通过的标记{maze->a[curpos.y][curpos.x]='@';return OK;}PosType NextPos(PosType curpos,int di)//返回当前位置的下一位置{PosType pos=curpos;switch(di){case 1: //右东pos.x++;break;case 2: //下南pos.y++;break;case 3: //左西pos.x--;break;case 4: //上北pos.y--;break;}return pos;}//若迷宫有解,则求得一条存放在栈中(从栈底到栈顶),并返回OK,否则返回ERROR int MazePath(MazeType *maze,PosType start,PosType end){PosType curpos;SqStack *S=(SqStack *)malloc(sizeof(SqStack));InitStack(S);SElemType *e;e=(SElemType *)malloc(sizeof(SElemType));curpos=start; //设定当前位置为入口位置int curstep = 1; //探索第一步do {if(Pass(maze,curpos)) //当前位置可通过{FootPrint(maze,curpos);e->ord=curstep;e->seat=curpos;e->di=1;Push(S,e);if(curpos.x==end.x&&curpos.y==end.y)return (OK);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=e->seat;curpos=NextPos(curpos,e->di);}}}}while(!StackEmpty(S));return ERROR;}void PrintMaze(MazeType *maze) //打印迷宫{int i,j,k,n;int c[999],d[999];for(i=0,k=0;i<=maze->hang;i++){for(j=0;j<=maze->lie;j++){printf("%c ",maze->a[i][j]);if(maze->a[i][j]=='*'){c[k]=i;d[k]=j;k++;}}printf("\n");}n=k;for(k=0;k<n;k++)printf("<%d,%d>",c[k],d[k]);printf("\n");printf("\n");}int main(){int zmg;char ch;printf(" 数据结构课程设计--迷宫问题求解\n\n");printf(" |----------------------------------------|\n");printf(" | |\n");printf(" | |\n");printf(" | |\n");printf(" | |\n");printf(" | XXXX XXXXXXXXXXXXXX |\n");printf(" | XXXXXXX |\n");printf(" |----------------------------------------|\n");getchar();do{system("cls");fflush(stdin);MazeType *maze=(MazeType *)malloc(sizeof(MazeType)); //设置迷宫的长宽不含外墙printf("请输入迷宫的列数(不含外墙时):");scanf("%d",&maze->lie);printf("请输入迷宫的行数(不含外墙时):");scanf("%d",&maze->hang);generatemaze(maze);printf("随机创建迷宫\n");PrintMaze(maze);getchar();getchar();PosType start,end;start.x=1;start.y=1;end.x=maze->lie-1;end.y=maze->hang-1;zmg=MazePath(maze,start,end);if(zmg){printf("此迷宫通路为\n");PrintMaze(maze);}elseprintf("此迷宫无通路\n"); //getchar();printf("再次尝试?(Y/N)?");scanf("%c",&ch);}while(ch=='Y'||ch=='y');return 0;}四、调试分析1.本程序界面设计合理,以空格为通路,感叹号!为障碍,笑脸为起始点,*为行走路线,心形为出口设计精巧,便于用户使用。
数据结构栈的应用(迷宫求解)

数据结构栈的应用(迷宫求解)栈的应用迷宫求解任务:可以输入一个任意大小的迷宫数据,用非递归的方法求出一条走出迷宫的路径,并将路径输出;源代码:#include#include/*数据定义*/typedefenum { ERROR, OK } Status;typedefstruct{int row; //row表示"行"号int line; //line表示"列"号}PosType; //位置的元素类型typedefstruct{intord; //该通道在路径上的"序号"PosType seat; //通道块在迷宫中的"坐标位置"int di; //从此通道走向下以通道块的"方向"}SElemType; //栈的元素类型typedefstruct{SElemType * base;SElemType * top;intstacksize;}SqStack;/*函数原型说明*/Status InitStack(SqStack&S); //创建一个空栈SStatus Push(SqStack&S,SElemType&a); //插入新元素aStatus Pop(SqStack&S,SElemType&a); //删除栈顶元素,a返回其值Status StackEmpty(SqStack S); //检查是否空栈Status MazePath(int maze[12][12],SqStack&S, PosType start, PosType end); //找通路void Initmaze(int maze[12][12],intx,int y); //初始化迷宫void printmaze(int maze[12][12],intx,int y); //显示迷宫Status Pass(int maze[12][12],PosTypeCurPos); //判断当前位置是否可通void Markfoot(int maze[12][12], PosTypeCurPos); //标记当前位置不可通PosTypeNextPos(PosTy peCurPos, intDir); //进入下一位置void printpath(int maze[12][12],SqStackS,intx,inty,PosTypestart,PosType end); //显示通路/*主函数*/void main (void){PosTypestart,end;int t=0;SqStack S;int maze[12][12];do{if(t!=0)printf("\n");for(int i=0;i<20;i++){printf("* ");}if(t==0){printf("\n*\t欢迎来到迷宫世界\t * \n"); printf("*\t 1.设置迷宫\t\t * \n");}else{printf("\n* 请继续选择:\t\t\t * \n");printf("*\t 1.设置迷宫\t\t * \n");}printf("*\t 2.设置迷宫路径\t\t * \n");printf("*\t 3.输出迷宫通路路径\t * \n"); printf("*\t 4.结束\t\t\t *\n");t++;for(int j=0;j<20;j++){printf("* ");}printf("\n");int s;printf("请选择:");scanf("%d",&s);intaa;intx,y;switch(s){case 1://1.初始化迷宫aa=1;printf("迷宫设置:请输入\n"); //设置迷宫大小do{printf("行X(x<10)=");scanf("%d",&x);printf("列Y(y<10)=");scanf("%d",&y);if(x<1 || x>10||y<1 || y>10){printf("输入错误!\n");}}while(x<1 || x>10||y<1 || y>10);Initmaze(maze,x,y); //初始化迷宫printmaze(maze,x,y); //显示所创建的迷宫break;case 2://寻找迷宫路径if(aa==1){aa=2; //设置入口和出口do{printf("输入入口行坐标:");scanf("%d",&start.row); if(start.row>x)printf("输入错误!\n");}while(start.row>x);do{printf("输入入口列坐标:");scanf("%d",&start.line); if(start.line>y)printf("输入错误!\n");}while(start.line>y);do{printf("输入出口行坐标:");scanf("%d",&end.row); if(end.row>x)printf("输入错误!\n");}while(end.row>x);do{printf("输入出口列坐标:");scanf("%d",&end.line);if(end.line>y)printf("输入错误!\n");}while(end.line>y);}elseprintf("请先初始化迷宫!\n");printf("\n所设置入口坐标为(%d,%d),出口坐标为(%d,%d).\n",start.row,start.line,end.row,end.line);break;case 3://输出此迷宫通路路径if(aa==2){aa=3;if(MazePath(maze,S,start,end)) //若有通路,显示通路printpath(maze,S,x,y,start,end);elseprintf("\n抱歉,找不到通路!\n\n");elseprintf("请先初始化迷宫并寻找路径!\n");break;case 4:exit(0);break;default:exit(0);}}while(1);}/*若迷宫maze中从入口start到出口end的通道,则求得一条存放在栈中*/Status MazePath(int maze[12][12],SqStack&S, PosType start, PosType end){PosTypecurpos;intcurstep;SElemType e;InitStack(S);curpos = start; // 设定"当前位置"为"入口位置curstep = 1; // 探索第一步do {if (Pass(maze,curpos)) // 当前位置可通过,即是未曾走到过的通道块{Markfoot(maze,curpos); // 留下足迹e.di =1;e.ord = curstep;e.seat= curpos;Push(S,e); // 加入路径if (curpos.row==end.row&&curpos.line==end.line)return OK; // 到达终点(出口)curpos = NextPos(curpos, 1); // 下一位置是当前位置的东邻curstep++; // 探索下一步}else // 当前位置不能通过{if (!StackEmpty(S)){Pop(S,e);while (e.di==4 && !StackEmpty(S)){Markfoot(maze,e.seat); // 留下不能通过的标记,并退回一步Pop(S,e);}if (e.di<4)e.di++; // 换下一个方向探索Push(S, e);curpos = NextPos(e.seat, e.di); // 当前位置设为新方向的相邻块}}}} while (!StackEmpty(S));return ERROR;}/*初始化迷宫时间复杂度:n^2*/voidInitmaze(int maze[12][12],intx,int y){char select;do{printf("创建方式A:自动生成B:手动创建\n");label: s canf("%c",&select);if(select=='a'||select=='A') //自动生成{for(int i=1;i<x+1;i++)< p="">{for(int j=1;j<y+1;j++)< p="">maze[i][j]=rand()%2;}break;}else if(select=='b'||select=='B') //手动设置{printf("按行输入%d*%d数据,0代表可通,1代表不可通(每行以Enter结束):\n",x,y);for(int i=1;i<x+1;i++)< p="">{for(int j=1;j<y+1;j++)< p="">scanf("%d",&maze[i][j]);maze[i][y+1]=1;}for(i=0;i< p="">break;}else if(select=='\n')goto label; //排除Enter键的影响elseif(select!='b'||select!='B'||select!='a'||select!='A')printf("输入错误!\n");}while(select!='b'||select!='B'||select!='a'||select!='A');}/*显示迷宫时间复杂度:n^2*/voidprintmaze(int maze[12][12],intx,int y){printf("\n\n");printf("显示所建的迷宫(#表示墙):\n");printf("%c ",'#');printf("%c ",'y');for(int j=1;j<y+1;j++)< p="">printf("%d ",j);printf("%c ",'#');printf("\nx ");for(int i=1;i<y+3;i++)< p=""> printf("%c ",'#');printf("\n");for(i=1;i<x+1;i++)< p=""> {if(i<x+1)< p="">printf("%d ",i);elseprintf("%c ",'#');printf("%c ",'#');for(int j=1;j<y+1;j++)< p=""> {printf("%d ",maze[i][j]);}printf("%c",'#');printf("\n");}for(i=0;i</y+1;j++)<></x+1)<></x+1;i++)<></y+3;i++)<></y+1;j++)<><></y+1;j++)<></x+1;i++)<></y+1;j++)<></x+1;i++)<>。
C语言数据结构之迷宫问题

C语⾔数据结构之迷宫问题本⽂实例为⼤家分享了数据结构c语⾔版迷宫问题栈实现的具体代码,供⼤家参考,具体内容如下程序主要参考⾃严蔚敏⽼师的数据结构c语⾔版,在书中程序的⼤体框架下进⾏了完善。
关于迷宫问题的思路可查阅原书。
#include<iostream>using namespace std;#define MAXSIZE 10typedef int Status;typedef struct{int x;int y;}Postype;typedef struct{int ord;Postype seat;int dir;}SElemType;//栈的元素类型typedef struct{//SElemType data[MAXSIZE];SElemType* top;SElemType* base;}Stack;//栈的结构类型typedef struct{char arr[MAXSIZE][MAXSIZE];}MAZETYPE;//迷宫结构体MAZETYPE maze;void InitMaze(){maze.arr[0][0] = maze.arr[0][1] = maze.arr[0][2] = maze.arr[0][3] = maze.arr[0][4] = maze.arr[0][5] = maze.arr[0][6] = maze.arr[0][7] = maze.arr[0][8] = maze.arr[0][9] = '1'; maze.arr[1][0] = maze.arr[1][3] = maze.arr[1][7] = maze.arr[1][9] = '1';maze.arr[1][1] = maze.arr[1][2] = maze.arr[1][4] = maze.arr[1][5] = maze.arr[1][6] = maze.arr[1][8] = '0';maze.arr[2][0] = maze.arr[2][3] = maze.arr[2][7] = maze.arr[2][9] = '1';maze.arr[2][1] = maze.arr[2][2] = maze.arr[2][4] = maze.arr[2][5] = maze.arr[2][6] = maze.arr[2][8] = '0';maze.arr[3][0] = maze.arr[3][5] = maze.arr[3][6] = maze.arr[3][9] = '1';maze.arr[3][1] = maze.arr[3][2] = maze.arr[3][3] = maze.arr[3][4] = maze.arr[3][7] = maze.arr[3][8] = '0';maze.arr[4][0] = maze.arr[4][2] = maze.arr[4][3] = maze.arr[4][4] = maze.arr[4][9] = '1';maze.arr[4][1] = maze.arr[4][5] = maze.arr[4][6] = maze.arr[4][7] = maze.arr[4][8] = '0';maze.arr[5][0] = maze.arr[5][4] = maze.arr[5][9] = '1';maze.arr[5][1] = maze.arr[5][2] = maze.arr[5][3] = maze.arr[5][5] = maze.arr[5][6] = maze.arr[5][7] = maze.arr[5][8] = '0';maze.arr[6][0] = maze.arr[6][2] = maze.arr[6][6] = maze.arr[6][9] = '1';maze.arr[6][1] = maze.arr[6][3] = maze.arr[6][4] = maze.arr[6][5] = maze.arr[6][7] = maze.arr[6][8] = '0';maze.arr[7][0] = maze.arr[7][2] = maze.arr[7][3] = maze.arr[7][4] = maze.arr[7][6] = maze.arr[7][9] = '1';maze.arr[7][1] = maze.arr[7][5] = maze.arr[7][7] = maze.arr[7][8] = '0';maze.arr[8][0] = maze.arr[8][1] = maze.arr[8][9] = '0';maze.arr[8][2] = maze.arr[8][3] = maze.arr[8][4] = maze.arr[8][5] = maze.arr[8][6] = maze.arr[8][7] = maze.arr[8][8] = '0';maze.arr[9][0] = maze.arr[9][1] = maze.arr[9][2] = maze.arr[9][3] = maze.arr[9][4] = maze.arr[9][5] = maze.arr[9][6] = maze.arr[9][7] = maze.arr[9][8] = maze.arr[9][9] = '1'; }Status initStack(Stack &s){s.base = (SElemType*)malloc(MAXSIZE*sizeof(SElemType));if (!s.base) return 0;s.top = s.base;return 1;}void Push(Stack &s, SElemType e){*s.top++ = e;}void Pop(Stack &s, SElemType &e){e = *--s.top;}Status StackEmpty(Stack &s){if (s.top == s.base) return 1;else return 0;}Status Pass(Postype curpos){if (maze.arr[curpos.x][curpos.y] == '0')return 1;else return 0;}void Foot(Postype curpos){maze.arr[curpos.x][curpos.y] = '*';}void MarkPrint(Postype curpos){maze.arr[curpos.x][curpos.y] = '!';}Status StructCmp(Postype a, Postype b){if (a.x = b.x&&a.y == b.y) return 1;else return 0;}//下⼀个位置Postype NextPos(Postype CurPos, int Dir){Postype ReturnPos;switch (Dir){case 1:ReturnPos.x = CurPos.x;ReturnPos.y = CurPos.y + 1;break;case 2:ReturnPos.x = CurPos.x + 1;ReturnPos.y = CurPos.y;break;case 3:ReturnPos.x = CurPos.x;ReturnPos.y = CurPos.y - 1;break;case 4:ReturnPos.x = CurPos.x - 1;ReturnPos.y = CurPos.y;break;}return ReturnPos;}Status MazePath(Postype start, Postype end) {Stack s;SElemType e;initStack(s);Postype curpos = start;int curstep = 1;do{if (Pass(curpos)){Foot(curpos);e = { curstep, curpos, 1 };Push(s, e);if (StructCmp(curpos, end)) return 1;curpos = NextPos(curpos, 1);curstep++;}else{if (!StackEmpty(s)){Pop(s, e);while (e.dir ==4 &&!StackEmpty(s)){MarkPrint(e.seat); Pop(s, e);}if (e.dir < 4 && !StackEmpty(s)){e.dir++;Push(s, e);curpos = NextPos(e.seat, e.dir);}}}} while (!StackEmpty(s));return 0;}int main(){InitMaze();Postype s, e;s.x = s.y = 1;e.x = e.y = 8;if (MazePath(s, e))printf("迷宫成功解密!\n");elseprintf("解密失败\n");for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){printf("%c ", maze.arr[i][j]);}printf("\n");}cout << "-=================================" << endl;for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){if (maze.arr[i][j] == '*' || maze.arr[i][j] == '!')printf("%c ", maze.arr[i][j]);else cout << " ";}printf("\n");}}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
数据结构迷宫问题完整代码

int main(int argc, char* argv[])
{
struct SqStack *p=Init();
int a[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},
printf("%d,%d,%d,%d\n",e.ord,e.seat.x,e.seat.y,e.di);
}
}
int Pass(struct PosType cur,int b[10][10])
{
if(b[cur.x][cur.y]==1)
return 0;
else return 1;
#include "stdafx.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include <malloc.h>
#include <stdlib.h>
struct PosType{
int x;
int y;
};
int curstep=1;
do
{
if(Pass(curpos,a))
{
FootPrint(curpos,a);
e.di=1;
e.ord=curstep;
e.seat=curpos;
Push(p,e);
printf("%d,%d,%d,%d\n",e.ord,e.seat.x,e.seat.y,e.di);
用c语言实现迷宫求解完美源代码

用c语言实现迷宫求解完美源代码#include#include#include#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2#define UNDERFLOW -2typedef int Status;//-----栈开始-----typedef struct{//迷宫中r行c列的位置int r;int c;}PostType;//坐标位置类型typedef struct{int ord;// 当前位置在路径上的序号PostType seat;// 当前坐标int di;// 从此通块走向下一通块的“方向”}SElemType;// 栈的元素类型//定义链式栈的存储结构struct LNode{SElemType data;//数据域struct LNode *next;//指针域};struct LStack{struct LNode *top;//栈顶指针};Status InitStack(LStack &s)//操作结果:构造一个空栈S {struct LNode *p;p=(LNode *)malloc(sizeof(LNode));if(!p){printf("分配失败,退出程序");exit(ERROR);}s.top=p;p->next=NULL;return OK;}Status StackEmpty(LStack s)//若栈s为空栈,则返回TRUE,否则FALSE{if(s.top->next==NULL) return TRUE;return FALSE;}Status Push(LStack &s,SElemType e)//插入元素e成为新的栈顶元素{struct LNode *p;p=(LNode *)malloc(sizeof(LNode));if(!p) exit(OVERFLOW);s.top->data=e;p->next=s.top;s.top=p;return OK;}Status Pop(LStack &s,SElemType &e)//删除s的栈顶元素,并且用e返回其值{struct LNode *p;if(!(s.top->next)) exit(UNDERFLOW);p=s.top;s.top=p->next;e=s.top->data;free(p);return OK;}Status DestroyStack(LStack &s)//操作结果:栈s被销毁{struct LNode *p;p=s.top;while(p){s.top=p->next;free(p);p=s.top;}return OK;}//-----栈结束------//-----迷宫开始-------#define MAXLEN 10// 迷宫包括外墙最大行列数typedef struct{int r;int c;char adr[MAXLEN][MAXLEN];// 可取' ''*' '@' '#'}MazeType;// 迷宫类型Status InitMaze(MazeType&maze){// 初始化迷宫,成功返回TRUE,否则返回FALSE int m,n,i,j;printf("输入迷宫行数和列数(包括了外墙): ");scanf("%d%d",&maze.r,&maze.c); // 输入迷宫行数和列数for(i=0;i<=maze.c+1;i++){// 迷宫行外墙maze.adr[0][i]='#';maze.adr[maze.r+1][i]='#';}//forfor(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]=' ';// 初始化迷宫printf("输入障碍的坐标((-1 -1)结束): ");scanf("%d%d",&m,&n);// 接收障碍的坐标while(m!=-1){if(m>maze.r || n>maze.c)// 越界exit(ERROR);maze.adr[m][n]='#';// 迷宫障碍用#标记printf("输入障碍的坐标((-1,-1)结束): ");scanf("%d%d",&m,&n);}//whilereturn OK;}//InitMazeStatus Pass(MazeType maze,PostType curpos){// 当前位置可同则返回TURE,否则返回FALSEif(maze.adr[curpos.r][curpos.c]==' ')// 可通return TRUE;elsereturn FALSE;}//PassStatus FootPrint(MazeType &maze,PostType curpos) {// 若走过并且可通,则返回TRUE,否则返回FALSE maze.adr[curpos.r][curpos.c]='*';//"*"表示可通return OK;}//FootPrintPostType NextPos(PostType &curpos,int i){// 指示并返回下一位置的坐标PostType cpos;cpos=curpos;switch(i){//1.2.3.4 分别表示东南西北方向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(ERROR);}return cpos;}//NextposStatus MarkPrint(MazeType &maze,PostType curpos) {// 曾走过,但不是通路标记,并返回OKmaze.adr[curpos.r][curpos.c]='@';//"@" 表示曾走过但不通return OK;}//MarkPrintStatus MazePath(MazeType &maze,PostType start,PostType end){// 若迷宫maze存在通路,则求出一条同路放在栈中,并返回TRUE,否则返回FALSE struct LStack S;PostType curpos;int curstep;// 当前序号,1,2,3,4分别表示东南西北方向SElemType e;InitStack(S);curpos=start; //设置"当前位置"为"入口位置"curstep=1;// 探索第一位printf("以三元组形式表示迷宫路径:\n");do{if(Pass(maze,curpos)){// 当前位置可以通过FootPrint(maze,curpos);// 留下足迹e.ord=curstep;e.seat=curpos;e.di=1;printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);Push(S,e);// 加入路径if(curpos.r==end.r&&curpos.c==end.c)if(!DestroyStack(S))// 销毁失败exit(OVERFLOW);elsereturn TRUE; // 到达出口else{curpos=NextPos(curpos,1);// 下一位置是当前位置的东邻curstep++;// 探索下一步}//else}//ifelse{// 当前位置不通时if(!StackEmpty(S)){Pop(S,e);while(e.di==4&& !StackEmpty(S)){MarkPrint(maze,e.seat);Pop(S,e);// 留下不能通过的标记,并退一步}//whileif(e.di < 4){e.di++;// 换一个方向探索Push(S,e);curpos=NextPos(e.seat,e.di);// 设定当前位置是该方向上的相邻printf("%d %d %d-->",e.seat.r,e.seat.c,e.di);}//if}//if}//else}while(!StackEmpty(S));if(!DestroyStack(S))// 销毁栈exit(OVERFLOW);elsereturn FALSE;}//MazePathvoid PrintMaze(MazeType &maze){// 将标记路径信息的迷宫输出到终端int i,j;printf("\n输出迷宫(*表示通路):\n\n");printf("");for(i=0;i<=maze.r+1;i++)// 打印列数名printf("%4d",i);printf("\n\n");for(i=0;i<=maze.r+1;i++){printf("%2d",i);// 打印行名for(j=0;j<=maze.c+1;j++)printf("%4c",maze.adr[i][j]);// 输出迷宫当前位置的标记printf("\n\n");}}//PrintMazeint main(){// 主函数MazeType maze;PostType start,end;char cmd;do{printf("-------创建迷宫--------\n");if(!InitMaze(maze)){printf("Initialization errors\n");exit(OVERFLOW);// 初始化失败}do{// 输入迷宫入口坐标printf("\n输入迷宫入口坐标: ");scanf("%d%d",&start.r,&start.c);if(start.r>maze.r ||start.c>maze.c){printf("\nBeyond the maze\n"); continue;}}while(start.r>maze.r ||start.c>maze.c);do{// 输入迷宫出口坐标printf("\n输入迷宫出口坐标: ");scanf("%d%d",&end.r,&end.c);if(end.r>maze.r ||end.c>maze.c){printf("\nBeyond the maze\n"); continue;}}while(end.r>maze.r ||end.c>maze.c);if(!MazePath(maze,start,end))//迷宫求解printf("\nNo path from entranceto exit!\n"); elsePrintMaze(maze);// 打印路径printf("\n需要继续创建新的迷宫吗?(y/n): "); scanf("%s",&cmd);}while(cmd=='y' || cmd=='Y');}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <stdio.h>#include <stdlib.h>#include <string.h>#define STACK_INIT_SIZE 100#define INCREMENT 10typedef struct{int r;int c;}zuobiao;typedef struct{int ord; //在当前坐标上的“标号”zuobiao seat; //坐标int di; //走向下一通道的方向}lujing;typedef struct{int sz[10][10];}Maze;typedef struct SqStack{lujing *base;lujing *top;int size;}SqStack;int initStack(SqStack *s){s->base = (lujing *)malloc(STACK_INIT_SIZE*sizeof(lujing) );if(!s->base)return -1;s->top = s->base;s->size = STACK_INIT_SIZE;return 0;}int push(SqStack *s, lujing e){if(s->top - s->base >= s->size){s->base = (lujing *)realloc(s->base, (s->size+INCREMENT)*sizeof(lujing));if(!s->base)return -1;s->top = s->base+s->size;s->size += INCREMENT;}*s->top++ = e;return 0;}int pop(SqStack *s,lujing *e){if(s->top == s->base)return -1;*e = *(--s->top);return 0;}int isEmpty(SqStack *s){if(s->base == s->top)return 1;elsereturn 0;}int pass( Maze maze,zuobiao dqzb) {if (maze.sz[dqzb.r][dqzb.c]==1)return 1; // 如果当前位置是可以通过,返回1else return 0; // 否则返回0}void footPrint(Maze &maze,zuobiao dqzb) {maze.sz[dqzb.r][dqzb.c]=9;}void markPrint(Maze &maze,zuobiao dqzb) {maze.sz[dqzb.r][dqzb.c]=4;}zuobiao nextPos(zuobiao dqzb, int Dir) {zuobiao xzb;switch (Dir) {case 1:xzb.r=dqzb.r;xzb.c=dqzb.c+1;break;case 2:xzb.r=dqzb.r+1;xzb.c=dqzb.c;break;case 3:xzb.r=dqzb.r;xzb.c=dqzb.c-1;break;case 4:xzb.r=dqzb.r-1;xzb.c=dqzb.c;break;}return xzb;}int mazePath(Maze &maze, zuobiao start, zuobiao end){SqStack *s = (SqStack *)malloc(sizeof(SqStack));initStack(s);zuobiao dqzb = start; // 设定"当前位置"为"入口位置"lujing e;int curstep = 1; // 探索第一步do {if (pass(maze,dqzb)) { // 当前位置可通过,即是未曾走到过的通道块footPrint(maze,dqzb); // 留下足迹e.di =1;e.ord = curstep;e.seat= dqzb;push(s,e); // 加入路径if (dqzb.r == end.r && dqzb.c==end.c)return 0; // 到达终点(出口)dqzb = nextPos(dqzb, 1); // 下一位置是当前位置的东邻curstep++; // 探索下一步} else { // 当前位置不能通过if (!isEmpty(s)) {pop(s,&e);while (e.di==4 && !isEmpty(s)) {markPrint(maze,e.seat);pop(s,&e); // 留下不能通过的标记,并退回一步}if (e.di<4) {e.di++; // 换下一个方向探索push(s, e);dqzb = nextPos(e.seat, e.di); // 当前位置设为新方向的相邻块}}}} while (!isEmpty(s) );return -1;}void main(){printf(" *----Δ迷宫求解Δ----*\n\n");printf("---迷宫如下所示:(说明:0表示墙;1表示可以通过)");printf("\n");Maze maze; //声明mazemaze.sz[0][0]= 0;maze.sz[0][1]= 0;maze.sz[0][2]= 0;maze.sz[0][3]= 0;maze.sz[0][4]= 0;maze.sz[0][5]= 0;maze.sz[0][6]= 0;maze.sz[0][7]= 0;maze.sz[0][8]= 0;maze.sz[0][9]= 0;maze.sz[1][0]= 0;maze.sz[1][1]= 1;maze.sz[1][2]= 1;maze.sz[1][3]= 0;maze.sz[1][4]= 1;maze.sz[1][5]= 1;maze.sz[1][6]= 1;maze.sz[1][7]= 0;maze.sz[1][8]= 1;maze.sz[1][9]= 0;maze.sz[2][0]= 0;maze.sz[2][1]= 1;maze.sz[2][2]= 1;maze.sz[2][3]= 0;maze.sz[2][4]= 1;maze.sz[2][5]= 1;maze.sz[2][6]= 1;maze.sz[2][7]= 0;maze.sz[2][8]= 1;maze.sz[2][9]= 0;maze.sz[3][0]= 0;maze.sz[3][1]= 1;maze.sz[3][2]= 1;maze.sz[3][3]= 1;maze.sz[3][4]= 1;maze.sz[3][5]= 0;maze.sz[3][6]= 0;maze.sz[3][7]= 1;maze.sz[3][8]= 1;maze.sz[3][9]= 0;maze.sz[4][0]= 0;maze.sz[4][1]= 1;maze.sz[4][2]= 0;maze.sz[4][3]= 0;maze.sz[4][4]= 0;maze.sz[4][5]= 1;maze.sz[4][6]= 1;maze.sz[4][7]= 1;maze.sz[4][8]= 1;maze.sz[4][9]= 0;maze.sz[5][0]= 0;maze.sz[5][1]= 1;maze.sz[5][2]= 1;maze.sz[5][3]= 1;maze.sz[5][4]= 0;maze.sz[5][5]= 1;maze.sz[5][6]= 1;maze.sz[5][7]= 0;maze.sz[5][8]= 1;maze.sz[5][9]= 0;maze.sz[6][0]= 0;maze.sz[6][1]= 1;maze.sz[6][2]= 0;maze.sz[6][3]= 1;maze.sz[6][4]= 1;maze.sz[6][5]= 1;maze.sz[6][6]= 0;maze.sz[6][7]= 1;maze.sz[6][8]= 1;maze.sz[6][9]= 0;maze.sz[7][0]= 0;maze.sz[7][1]= 1;maze.sz[7][2]= 0;maze.sz[7][3]= 0;maze.sz[7][4]= 0;maze.sz[7][5]= 1;maze.sz[7][6]= 0;maze.sz[7][7]= 0;maze.sz[7][8]= 1;maze.sz[7][9]= 0;maze.sz[8][0]= 0;maze.sz[8][1]= 0;maze.sz[8][2]= 1;maze.sz[8][3]= 1;maze.sz[8][4]= 1;maze.sz[8][5]= 1;maze.sz[8][6]= 1;maze.sz[8][7]= 1;maze.sz[8][8]= 1;maze.sz[8][9]= 0;maze.sz[9][0]= 0;maze.sz[9][1]= 0;maze.sz[9][2]= 0;maze.sz[9][3]= 0;maze.sz[9][4]= 0;maze.sz[9][5]= 0;maze.sz[9][6]= 0;maze.sz[9][7]= 0;maze.sz[9][8]= 0;maze.sz[9][9]= 0;int i,j,sum;for (i = 0; i < 10; i++) {for (j = 0; j < 10; j++){printf("%4d",maze.sz[i][j]);sum++;}if(sum%10==0)printf("\n");}printf("\n");printf("---请输入1求解路径图:");int a;scanf("%d",&a);if(a==1){zuobiao rukou,chukou;rukou.r=1;rukou.c=1;chukou.r=8;chukou.c=8;mazePath(maze,rukou,chukou);printf("\n");printf("---图解如下所示:(说明:0表示墙;1表示可以通过;4表示走不通;9表示所走的路径)\n");for (i = 0; i < 10; i++) {for (j = 0; j < 10; j++){printf("%4d",maze.sz[i][j]);sum++;}if(sum%10==0)printf("\n");}}}。