2022年迷宫算法集锦 .pdf





《2022年迷宫算法集锦 .pdf》由会员分享,可在线阅读,更多相关《2022年迷宫算法集锦 .pdf(29页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、#include#include typedef enum ERROR,OK Status;typedef struct int row;/row 表示行号int line;/line 表示列号PosType;/位置的元素类型typedef struct int ord;/该通道在路径上的 序号 PosType seat;/通道块在迷宫中的 坐标位置 int di;/从此通道走向下以通道块的方向 SelemType;/栈的元素类型typedef struct SelemType*base;SelemType*top;int stacksize;SqStack;/*创建一个空栈 S*/Statu
2、s InitStack(SqStack&S)S.base=(SelemType*)malloc(100*sizeof(SelemType);if(!S.base)return ERROR;S.top=S.base;S.stacksize=100;return OK;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 29 页 -/*插入新元素 a*/Status Push(SqStack&S,SelemType&a)*S.top+=a;return OK;/*删除栈顶元素,a 返回其值*/Status Pop(SqStack&S,SelemType&a)if(S.top=S.base
3、)return ERROR;a=*-S.top;return OK;/*检查是否空栈*/Status StackEmpty(SqStack S)if(S.top=S.base)return OK;else return ERROR;void Initmaze(int maze1212,int size)char select;printf(选择创建方式A:自动生成B:手动创建 n);label:scanf(%c,&select);if(select=a|select=A)/自动生成 for(int i=0;isize+2;i+)maze0i=1;for(i=1;isize+1;i+)mazei
4、0=1;for(int j=1;jsize+1;j+)mazeij=rand()%2;mazeisize+1=1;for(i=0;isize+2;i+)mazesize+1i=1;else if(select=b|select=B)/手动设置名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 29 页 -printf(按行输入%d*%d 数据,0 代表可通,1 代表不可通(每行以 Enter 结束):n,size,size);for(int i=0;isize+2;i+)maze0i=1;for(i=1;isize+1;i+)mazei0=1;for(int j=1;jsize+1;
5、j+)scanf(%d,&mazeij);mazeisize+1=1;for(i=0;isize+2;i+)mazesize+1i=1;else if(select=n)goto label;/排除 Enter键的影响else printf(输入错误!);/*显示迷宫*/void printmaze(int maze1212,int size)printf(nn);printf(显示所建的迷宫(#表示外面的墙):n);for(int i=0;isize+2;i+)printf(%c,#);printf(n);for(i=1;isize+1;i+)printf(%c,#);for(int j=1
6、;jsize+1;j+)printf(%d,mazeij);printf(%c,#);printf(n);for(i=0;iseat.rowp-seat.line=2;/标记为路径中的点p+;for(int i=0;isize+2;i+)printf(%c,#);printf(n);for(i=1;isize+1;i+)printf(%c,#);for(int j=1;jsize+1;j+)if(mazeij=2)printf(%c,0);else printf();printf(%c,#);printf(n);for(i=0;isize+2;i+)printf(%c,#);printf(nn
7、);/*判断当前位置是否可通*/Status Pass(int maze1212,PosType CurPos)if(mazeCurPos.rowCurPos.line=0)return OK;/如果当前位置是可以通过,返回1 else return ERROR;/其它情况返回 0 /*标记当前位置不可通*/void Markfoot(int maze1212,PosType CurPos)mazeCurPos.rowCurPos.line=1;名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 29 页 -/*进入下一位置*/PosType NextPos(PosType CurP
8、os,int Dir)PosType ReturnPos;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.ro
9、w=CurPos.row-1;ReturnPos.line=CurPos.line;break;return ReturnPos;/*若迷宫maze 中从入口start 到出口end 的通道,则求得一条存放在栈中*/Status MazePath(int maze1212,SqStack&S,PosType start,PosType end)PosType curpos;int curstep;SelemType e;InitStack(S);curpos=start;/设定当前位置 为入口位置名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 29 页 -curstep=1;/探
10、索第一步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&!Stack
11、Empty(S)Markfoot(maze,e.seat);/留下不能通过的标记,并退回一步Pop(S,e);if(e.di4)e.di+;/换下一个方向探索Push(S,e);curpos=NextPos(e.seat,e.di);/当前位置设为新方向的相邻块 while(!StackEmpty(S);return ERROR;名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 29 页 -void main()SqStack S;int size=0;/正方形迷宫尺寸int maze1212;/存储迷宫内路径可通情况for(int n=0;n10;n+)printf(创建一个正方
12、形迷宫,请输入迷宫尺寸(注意不要大于 10):n);/设置迷宫大小scanf(%d,&size);if(size10)printf(输入错误!);return;Initmaze(maze,size);/初始化迷宫printmaze(maze,size);/显示所创建的迷宫PosType start,end;/设置入口和出口printf(输入入口行坐标和列坐标:);scanf(%d,&start.row);scanf(%d,&start.line);printf(输入出口行坐标和列坐标:);scanf(%d,&end.row);scanf(%d,&end.line);if(MazePath(ma
13、ze,S,start,end)/若有通路,显示通路printpath(maze,S,size);else printf(找不到通路!nn);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 29 页 -一、需求分析1.选题理由本次课设我选择了迷宫问题,迷宫求解是数据结构课程的一个经典问题,迷宫问题要求寻找一条从入口到出口的路径。通常用的是“穷举求解”的方法。为了保证在任何位置上都能原路退回,显然需要用一个后进先出的结构来保存从入口到当前位置的路径。因此,在求解迷宫通路的算法中要应用“栈”的思想。对于栈的内容在整个学期的学习中我也有了一定的了解,所以选择了迷宫这一经典问题作为本次课设
14、的内容。2.基本原理分析迷宫问题通常是用“穷举求解”方法解决,即从入口出发,顺着某一个方向进行探索,若能走通,则继续往前走;否则沿着原路退回,换一个方向继续探索,直至出口位置,求得一条通路。假如所有可能的通路都探索到而未能到达出口,则所设定的迷宫没有通路。栈是一个后进先出的结构,可以用来保存从入口到当前位置的路径。以二维数组存储迷宫数据,通常设定入口点的下标为(1,1),出口点的下标为(n,n)。为处理方便起见,在迷宫的四周加一圈障碍。对于迷宫任何一个位置,均约定东、南、西、北四个方向可通。3.功能要求(1)以一个二维数组Mazem+2n+2 表示迷宫,其中:Maze0j和Mazem+1j(0
15、=j=n+1)及 Mazei0和 Mazein+1(0=i=m+1)为做外层的一圈障碍。数组中以0 表示通路,1 表示障碍,限定迷宫的大小为:m,n=10。(2)用户需用文件的形式输入迷宫的数据:文件中第一行的数据为迷宫的行数 m和列数 n;从第 2 行至第 m+1行(每行 n 个数)为迷宫值,用0,1 输入,同行中的两个数字之间用空白字符相隔。(3)迷宫的入口位置和出口位置可由用户随时设定。(4)若设定的迷宫存在通路,则以长方阵形式将迷宫及其通路输出到标准输出文件上,其中字符“#”表示障碍,“*”表示路径,“”表示曾途经该位置但不能到达出口,其余位置用空格符表示。若设定迷宫不存在通路则报告相
16、应信息(5)本程序只求出一条成功的通路。(6)程序执行的命令为:1,创建迷宫;2,求解迷宫;3,输出迷宫的解。二、详细设计源程序:#include#include#include#define MAXLEN 10/迷宫包括外墙最大行列数目#define TRUE 1 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 29 页 -#define FALSE 0#define OK 1#define ERROR 0 typedef int Status;/坐标位置类型typedef struct int r,c;PosType;/迷宫中 r 行 c 列的位置/迷宫类型typedef s
17、truct int r;int c;char arrMAXLENMAXLEN;/可取,*,#MazeType;typedef struct/int step;/当前位置在路径上的“序号”PosType seat;/当前的坐标位置 int di;/往下一坐标位置的方向SElemType;/结点类型typedef struct NodeType SElemType data;NodeType*next;NodeType,*LinkType;/栈类型typedef struct LinkType top;int stacksize;SqStack;PosType start;PosType end;
18、MazeType maze;bool found;/创建栈Status InitStack(SqStack&S)S.top=(LinkType)malloc(sizeof(NodeType);名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 29 页 -S.top-next=NULL;S.stacksize=0;return OK;/进栈Status Push(SqStack&S,SElemType&e)LinkType p;p=(NodeType*)malloc(sizeof(NodeType);p-data=e;p-next=S.top;S.top=p;S.stacksize+
19、;return OK;/判断是否为栈空Status StackEmpty(SqStack S)if(S.top-next=NULL)return OK;return ERROR;/出栈Status Pop(SqStack&S,SElemType&e)LinkType p;if(StackEmpty(S)return ERROR;p=S.top;e=p-data;S.top=S.top-next;S.stacksize-;free(p);return OK;/销毁栈Status DestroyStack(SqStack&S)LinkType p;while(S.top!=NULL)p=S.top
20、;名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 29 页 -S.top=S.top-next;free(p);/一个一个删除 if(S.top=NULL)return OK;else return ERROR;/曾走过但不是通路标记并返回OK Status MarkPrint(MazeType&maze,PosType curpos)maze.arrcurpos.rcurpos.c=;/表示曾走过但不通 return OK;/曾走过而且是通路标记并返回OK Status FootPrint(MazeType&maze,PosType curpos)maze.arrcurpos
21、.rcurpos.c=*;/*表示可通 return OK;/选择下一步的方向PosType NextPos(PosType&curpos,int i)PosType 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;return cpos;/判断当前位置是否可通Status Pass(MazeType&maze,PosType curpos)if(maze.arrc
22、urpos.rcurpos.c=)return TRUE;else return FALSE;名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 29 页 -/创建迷宫/按照用户输入的二维数组(0 或 1),设置迷宫 maze的初值,包括加上边缘一圈的值void InitMaze(MazeType&maze,char aMAXLENMAXLEN,int row,int col)maze.r=row;maze.c=col;for(int i=0;i=col+1;i+)a0i=1;arow+1i=1;for(i=0;i=row+1;i+)ai0=1;aicol+1=1;for(i=0;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年迷宫算法集锦 2022 迷宫 算法 集锦

限制150内