迷宫问题实验报告_第1页
迷宫问题实验报告_第2页
迷宫问题实验报告_第3页
迷宫问题实验报告_第4页
迷宫问题实验报告_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、武汉纺织大学数学与计算机学院数据结构课程设计报告迷宫问题求解学生姓名:学 号:班 级:指导老师:报告日期:一、   问题描述以一个m x n的长方矩阵表示迷宫,1和0分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出从入口到出口的通路,或者没有通路的结论。二、   需求分析1、    以二维数组maze1010表示迷宫,数组中以元素1表示通路,0表示障碍,迷宫的大小理论上可以不限制,但现在只提供10*10大小迷宫。2、  迷宫的入口和出口需由用户自行设置。3、  

2、; 以长方形矩阵的形式将迷宫及其通路输出,输出中“#”表示迷宫通路,“1”表示障碍。4、      本程序只求出一条成功的通路。但是只要对函数进行小量的修改,就可以求出其他全部的路径。5、      程序执行命令为:(1)输入迷宫;(2)、求解迷宫;(3)、输出迷宫。三、   概要设计1、 设定栈的抽象数据类型定义:adt zhan基本操作:initstack(sqstack &s)操作结果:构造一个空栈push(*s,*e)初始条件:栈已经存在操作结果:将

3、e所指向的数据加入到栈s中pop(*s,*e)初始条件:栈已经存在操作结果:若栈不为空,用e返回栈顶元素,并删除栈顶元素getpop(*s,*e)初始条件:栈已经存在操作结果:若栈不为空,用e返回栈顶元素stackempty(*s)初始条件:栈已经存在操作结果:判断栈是否为空。若栈为空,返回1,否则返回0adt zhan2、 设定迷宫的抽象数据类型定义adt migong基本操作: status print(mazetype maze);           /显示迷宫status pass(m

4、azetype maze,postype curpos);     /判断当前位置是否可通status footprint(mazetype &maze,postype curpos);/标记当前位置已经走过status markprint(mazetype &maze,postype curpos);  /标记当前位置不可通postype nextpos(postype curpos,directivetypedi); / 进入下一位置adt yanshu3、 本程序包括三个模块a、 主程序模块void m

5、ain()初始化;迷宫求解;迷宫输出;b、 栈模块实现栈的抽象数据类型c、 迷宫模块实现迷宫的抽象数据类型四、流程图将入口、出口位置传入方法里判断当前位置是否可通将元素入栈是否到达迷宫出口右边是否存在通路上边是否存在通路左边是否存在通路下边是否存在通路存储结点入栈循环结束,无解迷宫有解迷宫五、  数据结构 typedef struct /位置结构 int row; /行位置 int col; /列位置 postype;typedef struct /迷宫类型 int arr1010; mazetype; typedef structint step; /当前位置在路径上的&

6、quot;序号" postype seat; /当前的坐标位置 directivetype di; /往下一个坐标位置的方向 selemtype;typedef struct / 栈类型selemtype *base; /栈的尾指针 selemtype *top; /栈的头指针 int stacksize; /栈的大小 sqstack;六、调试结果和分析a) 测试结果实际程序执行过程如下图所示: 参考文献1 严蔚敏、吴伟民:数据结构(c语言版)m,清华大学出版社 2007年版2 谭浩强:c语言设计(第三版)m,清华大学出版社 2005年版心得体会通过这段时间的课程设计,本人对计算机的

7、应用,数据结构的作用以及c语言的使用都有了更深的了解。尤其是c语言的进步让我深刻的感受到任何所学的知识都需要实践,没有实践就无法真正理解这些知识以及掌握它们,使其成为自己的财富。在设计此程序时,刚开始感到比较迷茫,然后一步步分析,试着写出基本的算法,思路渐渐清晰,最后完成程序。当然也遇到不少的问题,也正是因为这些问题引发的思考给我带了收获。在遇到描写迷宫路径的算法时,我感到有些困难,后来经过一步步分析和借鉴书本上的穷举求解的算法,最后把算法写出来。这次课程设计让我更加深入了解很多方面的知识,如数组的运用等等。在程序的编写中不要一味的模仿,要自己去摸索,只有带着问题反复实践,才能熟练地掌握和运用

8、。附录、   源代码 #include <stdio.h> /头文件 #include <malloc.h> #include <stdlib.h> #include <string.h> #define ok 1 /宏定义 #define error 0 #define overflow -2 typedef int status; /函数的返回值 typedef int directivetype; /下一个通道方向 #define stack_init_size 500 /栈的最大值 #define stackincre

9、ment 10 /增量/-存储结构 typedef struct int row; int col; postype; typedef structint step; /当前位置在路径上的"序号" postype seat; /当前的坐标位置 directivetype di; /往下一个坐标位置的方向 selemtype;typedef struct selemtype *base; selemtype *top; int stacksize; sqstack;/栈的存储typedef structint arr1010; mazetype;/迷宫类型/-函数声明stat

10、us initstack(sqstack &s);status pop(sqstack &s,selemtype &e);status push(sqstack &s,selemtype e);status stackempty(sqstack s);status mazepath(mazetype &maze,postype start,postype end);status pass(mazetype maze,postype curpos);status footprint(mazetype &maze,postype curpos);po

11、stype nextpos(postype curpos,directivetype di);status markprint(mazetype &maze,postype curpos);status print(mazetype maze);void main()postype start,end; mazetype a;printf("请输入迷宫数据n");for(int i=0;i<10;i+) /接受输入的迷宫数据for(int j=0; j<10;j+)scanf("%d",&a.arrij); printf(&q

12、uot;请输入起始位置:行列 "); / 用户自定义起始点与终点scanf("%d%d",&start.row,&start.col);printf("请输入结束位置:行列 ");scanf("%d%d",&end.row,&end.col);if(mazepath(a,start,end) /寻找路径print(a);elseprintf("没有找到路径n");status initstack(sqstack &s)s.base=(selemtype *)mall

13、oc(stack_init_size*sizeof(selemtype); / 为栈申请存储地址if(!s.base) exit(overflow);s.top=s.base;s.stacksize=stack_init_size;return ok;/end initstackstatus pop(sqstack &s,selemtype &e)if(s.top=s.base)return error; e=*(s.top-1);/如果没有这句话就会在原地打转,导致在死胡同堵死s.top-;return ok;/end popstatus push(sqstack &

14、s,selemtype e)*s.top=e;s.top+;return ok;/end pushstatus stackempty(sqstack s)if(s.top=s.base)return ok;elsereturn error;/end stackemptystatus mazepath(mazetype &maze,postype start,postype end)postype curpos;curpos=start; int curstep=1;selemtype e;sqstack s;initstack(s);doif(pass(maze,curpos) /当前

15、位置可以通过,即未曾走过的模块 e.step=curstep; e.seat=curpos; e.di=1;footprint(maze,curpos);/留下足迹push(s,e); /加入到路径栈中if(curpos.col=end.col&&curpos.row=end.row)/达到终点(出口)return ok;curpos=nextpos(curpos,1);/下一位置是当前位置的东邻curstep+;/探索下一步/end ifelse /当前位置不能通过if(!stackempty(s)pop(s,e);while(e.di=4&&!stackem

16、pty(s)markprint(maze,e.seat);pop(s,e);/ 留下不能通过的标记,并回退一步/end whileif(e.di<4)e.di+;/ 换一个方向探索push(s,e);curpos=nextpos(e.seat,e.di);/设定当前位置是该新方向的相邻快/end if/end if/end elsewhile(!stackempty(s);/end ifprintf("nn");for(int i=0;i<10;i+)for(int j=0; j<10;j+)printf("%d ",maze.arri

17、j); printf("n"); return error;/end mazepathstatus pass(mazetype maze,postype curpos)if(maze.arrcurpos.rowcurpos.col=0) /判断当前路径对应数组值是否等于0,即当前路径是否可通return ok;elsereturn error;status footprint(mazetype &maze,postype curpos)maze.arrcurpos.rowcurpos.col=2;/为当前路径留下可以通过的标志return ok;/end passp

18、ostype nextpos(postype curpos,directivetype di)postype pos = curpos;switch(di)case 1:pos.col+; /向东寻找break;case 2:pos.row+; /向西寻找break;case 3:pos.col-; /向南寻找break;case 4:pos.row-; /向北寻找break;return pos;/end nextposstatus markprint(mazetype &maze,postype curpos)maze.arrcurpos.rowcurpos.col=3;/为当前路径留下不可通过的标志return ok;/end markprintstatus print(mazetype maze) /迷宫的输出显示int i,j;printf("n

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论