数据结构课程设计迷宫求解_第1页
数据结构课程设计迷宫求解_第2页
数据结构课程设计迷宫求解_第3页
数据结构课程设计迷宫求解_第4页
数据结构课程设计迷宫求解_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

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

文档简介

1、26湖南人文科技学院课程设计湖南人文科技学院计算机系数据结构课程设计课程名称:数据结构课程代码:题 目:迷宫求解年级/专业/班:09级计算机科学与技术1班学生姓名:学 号 :指导老师:开题时间:2010-12-21完成时间:2010-12-26目 录摘 要3abstract3一、引 言1二、设计目的与任务11、设计目的是12、设计任务是2三、设计方案与实施21、总体设计思想22、设计流程图33、详细设计44、程序清单45、程序调试与体会46、运行结果(截图)5五、致 谢13参考文献14附件14摘 要随着计算机的高速发展,计算机能很简便地解决很多问题。c语言编程也是解决问题的一种语言。而此我们的

2、数据结构程序设计是解决迷宫问题。求迷宫(老鼠吃奶酪)中从入口到出口的路径是一个经典的程序设计问题。“数据结构”成为计算机程序设计的重要理论技术基础,它不仅是计算机学科的核心课程,而且已成为其它理工专业的热门选修课。主要包括线性表、树和二叉树以及图等基本类型的数据结构。数据结构是一门研究非数值计算的程序设计问题中计算机的操作对象以及它们之间的关系和运算等的学科,包括数据的逻辑结构、数据的存储结构和数据的运算这三个方面的内容,其中逻辑结构可分为线性结构和非线性结构;存储结构可分为顺序存储和链式存储两类,图则属于逻辑结构中的非线性结构。广度优先搜索(bfs)用的队列一步一步完成的,从而找到的是最短路

3、径。关键词:队列,广度优先,搜索,最短路径,遍历abstractwith the rapid development of the computer,the computer can very easily solve many problems. c programming language is a language problem. our data structure and this program is designed to solve maze problems.find the maze(mouse eat cheese) to the exit path from the

4、entrance is a classic programming problem.data structure has become the important theory and the foundation of computer programming.it is not only the core curriculum of computer science,but also has became the hottest elective course of other tech professional. mainly including linear list, trees a

5、nd binary tree and graph, and other basic types of data structure.data structure is the study of the non-numerical calculation program design problem in computer operation objects and their relationship and operation,including data logical structure, data storage structure and the data of operation

6、this three aspects, and the logical structure can be divided into linear structure and nonlinear structure.storage structure can be divided into sequenced store and chain store two kinds.graph belongs to the logical structure of nonlinear structure.it is breadth-first search (bfs) with the queue for

7、 find the shortest path1、总体设计思想(1) 迷宫形状由0表示可通过,用1表示是障碍。为方便用0,1输入。并把迷宫图形保存在二维数组map中。而打印出的图形中 表示能过表示障碍.(2) 对探索过的位置加以标记used,输入起点终点后可由bfs()来完成搜索。到目的点就可退出该调用程序。把每步路径保存到mark内,通过反向进行退步可把完整的路径保存在结构体result数组re内,通过标记的路径可将串str作相应的改变就能输出的带路径的图。(3) 根据二维字符数组和加标记的位置坐标,输出迷宫的图形。(4) 该程序在获取迷宫图结构后,可对迷宫任意入口到出口的路线进行搜索,主要

8、由广度优先搜索完成该操作。它可以是100以内大小的迷宫,有自行提供的迷宫图,本课程设计是以二维数组作为迷宫的存储结构。而广度优先搜索(bfs)用的队列一步一步完成的,从而找到的是最短路径;该程序还能选择是4方向还是8方向的迷宫走法。并能输出打印2、设计流程图开始输入迷宫图形显示迷宫图形输入起点终点是否符合题意输出路径节点输出迷宫路线是否继续?是否更换迷宫结束3、详细设计struct pointint x,y,s;这个结构体是用来做广度搜索的对每个迷宫节点有相应的s(最短的步数,当然有些是不可达的)int move82 = 1,0,0,1,-1,0,0,-1,1,1,-1,-1,1,-1,-1,

9、1 ;/int bfs(int step)/ 广度搜索用来算出最短路径的走法 并将走法保存在re数组结构体中int initialization()/ 将str图形初始化int format_limit()/图形打印格式限制int print_maze_figure()/打印出图形int ppmf()/ 打印出迷宫图形int save_path()/ 将路径保存在str中并打印出路径迷宫图形int init()/ 从文件中植入数据 完成对map迷宫图的结构int judge()/判断输入的起点终点是否符合实际int main()/对上面函数的结合应用4、程序清单见附件5、程序调试与体会调试:在

10、我组的集体努力下,课程设计终于完成,由于我们对数据结构和c语言不是很了解,有时忽略了一些关键的细节,使得在编写程序的过程中出现了一些问题。对于打字有时粗心导致出现一些难以发现的小错误,在我们的耐心,细致的调试下最终使得程序能够运行,课程设计完满完工。1、问题一:求出起点到终点的路径 现象:求出的结果总是无任何现象原因:把相关重要的变量重复定义以至赋过的值被覆盖2、 问题二:常量转义字符现象:出现不正常字符常量原因:字符常量形式弄错3、问题三:输入起点终点时,未判断下标越界就使用数据现象: 原因:未判断下标越界就使用数据体会:在复杂的程序,都可以拆成一个个简单的小部分,逐个击破,再拼凑起来,在复

11、杂的事也变的简单了。由于本程序较大,调试时多人协作能更容易找出程序的错误并修改。6、运行结果(截图)将程序员录入后,让其运行。将会出现一个菜单的界面,执行各种操作均有其对应的代码。要执行何种操作只需输入对应的指令即可进行,在每步操作后均会有相应的提示。操作简单方便实用,下面即为一些操作的示意图。运行程序后出界面,运行结果如图所示。#include #include #include #include / 这个是stl 它是一个方便的东西 #include #include using namespace std;struct result int rx,ry;re100*100;int ri=

12、-1;struct pointint x,y,s;s,t;/s代表起点 t代表终点int mark100100; /用来标记怎么走到这个地方的 (保存的是方向的序号):0,1,2,3,4,5,6,7bool used100100;/标记已经走过的地方bool map100100;/输入0,1表示迷宫int move82 = 1,0,0,1,-1,0,0,-1,1,1,-1,-1,1,-1,-1,1 ;/int n,m;int bfs(int step) / 广度搜索用来算出最短路径的走法 并将走法保存在re结构体中queue my;s.s =0;while(!my.empty()my.pop(

13、);my.push(s);point temp,s1;while(!my.empty()s1 = my.front();my.pop();if(s1.x = t.x&s1.y=t.y)int u;re+ri.rx=s1.x; reri.ry=s1.y;printf(到目的地了n步数:%dn(%2d,%2d) - ,s1.s,s1.x,s1.y);for(int j = 1 ;j = s1.s;j+)u = s1.x;s1.x = s1.x - move marks1.xs1.y0;s1.y = s1.y - move markus1.y 1;re+ri.rx=s1.x; reri.ry=s1.

14、y;printf(%2d,%2d) - ,s1.x,s1.y);if(j+1)%5=0)printf(n);printf(n);system(pause); system(cls);return 1;for(int i =0 ;i step ; i+)temp.x = s1.x + movei0;temp.y = s1.y + movei1;temp.s = s1.s;if(temp.x=n|temp.y=n|usedtemp.xtemp.y|maptemp.xtemp.y)continue;elsemarktemp.xtemp.y = i;temp.s += 1 ;my.push(temp)

15、;usedtemp.xtemp.y = true;printf(不好意思,起点至终点无路径不可达!n);return 0;char str256*2563; /串保存图int initialization()/ 将str图形初始化int i1,j1,xj;for(i1=0;i1256*256;i1+) strcpy(stri1, );for(i1=0;i1n;i1+)for(j1=0,xj=0;xjn;j1=j1+2,xj+)if(mapi1xj = 0) strcpy(str2*i1*(2*n-1)+j1,);else strcpy(str2*i1*(2*n-1)+j1,);return 1

16、;int format_limit()/图形打印格式限制for(int i =0; i 0;i+)printf( );return 1;int print_maze_figure()/打印出图形int i,xi,xj;format_limit();for(i =0 ;i = n*2;i+)printf();printf(n);for(xi=0,xj=0;xj(2*n-1)*(2*n-1);xj+,xi+)if(0 = xi)format_limit();printf(); printf(%s,strxj); if(xi=2*n-2) printf(n);xi=-1; format_limit(

17、);for(i =0 ;i = n*2 ;i+)printf();printf(n);return 1;int ppmf()/ 打印出迷宫图形printf(tttt迷宫为nttt 表可走,表不可走 n);initialization();print_maze_figure();return 1;int save_path()/ 将路径保存在str中并打印出路径迷宫图形printf(ttt 迷宫路径图n(%d,%d)至(%d,%d)tt 表可走,表不可走 n,s.x,s.y,t.x,t.y);initialization();strcpy(str2*s.x*(2*n-1)+2*s.y,起);st

18、rcpy(str2*t.x*(2*n-1)+2*t.y,终);for(int wri=0;wriri;wri+) if(rewri.rxrewri+1.rx&rewri.ry=rewri+1.ry) strcpy(str2*rewri.rx*(2*n-1)+(2*n-1)+2*rewri.ry,); if(rewri.rx=rewri+1.rx&rewri.ryrewri+1.rx&rewri.ry=rewri+1.ry) strcpy(str2*rewri.rx*(2*n-1)-(2*n-1)+2*rewri.ry,); if(rewri.rx=rewri+1.rx&rewri.ryrewr

19、i+1.ry) strcpy(str2*rewri.rx*(2*n-1)+2*rewri.ry-1,); if(rewri.rxrewri+1.rx&rewri.ryrewri+1.ry) strcpy(str2*rewri.rx*(2*n-1)+(2*n-1)+1+2*rewri.ry,); if(rewri.rxrewri+1.ry) strcpy(str2*rewri.rx*(2*n-1)+(2*n-1)-1+2*rewri.ry,); if(rewri.rxrewri+1.rx&rewri.ryrewri+1.rx&rewri.ryrewri+1.ry) strcpy(str2*rew

20、ri.rx*(2*n-1)-(2*n-1)-1+2*rewri.ry,); print_maze_figure();return 1;/队列int init()/ 从文件中植入数据 完成对map迷宫图的结构file *pf;int i,j;pf = fopen(maze.txt,r);if(pf=null)return 0;fscanf(pf,%d,&n);for(i = 0;i n; i+)for(j = 0 ; j n; j+)usedij = false;fscanf(pf,%d,&mapij);fclose(pf);return 1;int judge()/判断输入的起点终点是否符合实

21、际bool flag = true;if(s.x=n|s.y=n)printf(起点越界,不符合!n);flag = false;else if(t.x=n|t.y=n)printf(终点越界,不符合!n);flag = false;else if(maps.xs.y)printf(起点是墙,不符合!n);flag = false;else if(mapt.xt.y)printf(终点是墙,不符合!n);flag = false;else if(s.x=t.x&s.y=t.y)printf(起点是终点,不符合!n);flag = false;if(!flag)printf(是则再输入否则退出程

22、序:(y/n)n);char ch20;scanf(%s,ch);if(ch0 = y|ch0 =y)return 1;else return 0;return 2;int main()char ch;int i,j,step;printf(tttn);next:system(pause); system(cls); printf(是否使用系统提供的迷宫图:(y/n)n); ch = getch(); if(ch = y|ch =y) init(); else printf(请输入迷宫的大小:(n*n);scanf(%d,&n);printf(t请输入迷宫的结构0,1表示0是路1是墙n);fo

23、r(i = 0;i n; i+)for(j = 0 ; j n; j+)usedij = false;scanf(%d,&mapij); bool flag=true;while(flag)for(i =0 ;i n;i+)for(j =0 ;j n ;j+)usedij = false;ri = -1;system(pause);system(cls);printf(是否显示原始迷宫:(y/n)n);ch = getch();if(ch = y|ch =y)ppmf();again:printf(请输入起点与终点:(x1 y1 x2 y2);scanf(%d %d %d %d,&s.x,&s

24、.y,&t.x,&t.y);if(1=judge()goto again;elseif(0=judge()break;printf(请选择4方向还是8方向的迷宫:);scanf(%d,&step);if(8=step)step=8;else step = 4;system(pause);system(cls);bfs(step);save_path();printf(是否继续(y/n)n);ch = getch();if(ch != y&ch !=y) flag = false;if(flag)printf(是否更换迷宫(y/n)n);ch = getch();if(ch = y|ch =y)

25、goto next;return 0;/*测试例子50 0 0 0 01 1 1 1 00 0 0 0 00 1 1 1 10 0 0 0 00 0 4 4 4160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01 1 1 1 1 1 1 1 1 1 1 1 1 1 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 1 00 1 1 1 1 1 1 1 1 1 1 1 1 0 1 00 1 0 0 0 0 0 0 0 0 0 0 1 0 1 00 1 0 1 1 1 1 1 1 1 1 0 1 0 1 00 1 0 1 0 0 0 0 0 0 1 0 1 0 1 00

26、 1 0 1 0 1 1 1 1 0 1 0 1 0 1 00 1 0 1 0 1 0 1 1 0 1 0 1 0 1 00 1 0 1 0 1 0 0 0 0 1 0 1 0 1 00 1 0 1 0 1 1 1 1 1 1 0 1 0 1 00 1 0 1 0 0 0 0 0 0 0 0 1 0 1 00 1 0 1 1 1 1 1 1 1 1 1 1 0 1 00 1 0 0 0 0 0 0 0 0 0 0 0 0 1 00 1 1 1 1 1 1 1 1 1 1 1 1 1 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 8 6 40 1 0 1 0 1 0 1 0 1 0 1 0 1 0 11 0 1 0 1 0 1 0 1 0 1 0 1 0 1 00 1 0 1 0 1 0 1 0 1 0 1 0 1 0 11 0 1 0 1 0 1 0 1 0 1 0 1 0 1 00 1 0 1 0 1 0 1 0 1 0 1 0 1 0 11 0 1 0 1 0 1 0 1 0 1 0 1 0 1 00 1 0 1 0 1 0 1 0 1 0 1 0 1 0 11 0 1 0 1 0 1 0 1 0 1 0 1 0 1 00 1 0 1 0 1 0 1 0 1 0 1 0 1 0 11 0 1 0 1 0 1 0 1 0 1 0 1 0 1 00

温馨提示

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

评论

0/150

提交评论