




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上迷宫运行是的一些结果:以下迷宫是教材上p50 图-3.4的迷宫:接下:以上两个界面为程序运行时分割的两部分。编辑是遇到的困难:首先,经过多次调试,程序已经没有错误了,但是在运行后,迷宫路径总是不能完整表示出来。多次查找原因后才知道自己对一些函数结果状态代码定义错误。例如:将error、FALSE定义为-1、-2,。将其定义为0后则能正确表现迷宫路径。之后,我又想除了自己手动创建迷宫之外,能不能在一个结构类型中定义一个mazetype maze2.a【maxlen】【maxlen】,使其初始化并附值,当要用时直接调用就可以了,但是照这样做了之后,总是发现有错误。我想可能
2、是文件后缀名得原因,可能在cpp文件不能这样用,所以索性就部编辑了,让函数只有一个功能。迷宫程序源代码:/迷宫程序 2011/4/3#include<stdio.h> #include<stdlib.h>#include<string.h>#define overflow -1#define error 0#define TRUE 1#define FALSE 0#define ok 1#define max_stacksize 100#define increase_size 10#define maxlen 10typedef structint row
3、;int col;postype; /坐标位置类型typedef struct int ord;postype seat;int di;elemtype; /栈的元素类型typedef struct sqstackelemtype *base;elemtype *top;int stacksize;sqstack; /栈类型typedef struct mazetypeint row;int col;char amaxlenmaxlen;mazetype; /迷宫类型int Initmaze(mazetype &maze);int mazepath(mazetype &maze
4、,postype start,postype end);int Initstack(sqstack &s);int push(sqstack &s,elemtype e); int pop(sqstack &s,elemtype &e); int stackempty(sqstack &s); int gettop(sqstack &s,elemtype &e);int destroystack(sqstack &s);int pass(mazetype &maze,postype curpos);int footprin
5、t(mazetype &maze,postype curpos);int markprint(mazetype &maze,postype curpos); postype nextpos(postype &curpos,int i); void outputmaze(mazetype &maze);void main() int i,j;char cn; postype start,end; mazetype maze;dodoprintf("*手动创建迷宫*n");Initmaze(maze);printf("*迷宫输出图为*n
6、");outputmaze(maze);printf("n-请输入迷宫入口坐标:");scanf("%d%d",&start.row,&start.col);if(start.row>maze.row|start.col>maze.col)printf("-输入坐标有误!n");continue;while(start.row>maze.row|start.col>maze.col);printf("n-请输入迷宫出口坐标:");scanf("%d%d&q
7、uot;,&end.row,&end.col);if(!mazepath(maze,start,end)printf("n*迷宫没有出口路径!*n");elseprintf("*输出迷宫路径('*'表示迷宫通道路径,''表示曾走过但不能走通的标记)*n");outputmaze(maze);/case 2:/mazetype maze2;/maze2.amaxlenmaxlen=;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;/1,0
8、,0,0,0,1,1,0,0,1;1,0,1,1,1,0,0,0,0,1;1,0,0,0,1,0,0,0,0,1;1,0,1,0,0,0,1,0,0,1;/1,0,1,1,1,0,1,1,0,1;1,1,0,0,0,0,0,0,0,1;1,1,1,1,1,1,1,1,1,1;/printf("是否继续-y:继续 n:结束n");scanf("%s",&cn);while(cn='y'|cn='n');int Initmaze(mazetype &maze) /以数组形式创建迷宫,不包括行数和列数int i,
9、j,m,n;printf("*请输入所要创建的迷宫的行数和列数*n");scanf("%d%d",&maze.row,&maze.col);printf("-以0代表通路,以1代表障碍(行列数包括墙壁)nn");for(j=0;j<=maze.col+1;j+)maze.a0j='#'maze.amaze.row+1j='#'for(i=1;i<maze.row+1;i+)maze.ai0='#'maze.aimaze.col+1='#'for
10、(i=1;i<maze.row+1;i+)for(j=1;j<maze.col+1;j+)maze.aij='0'printf("-当输入的的横坐标为-1时表示结束nn");doprintf("请输入障碍坐标: ");scanf("%d%d",&m,&n);maze.amn='1'while(m!=-1); return ok;int Initstack(sqstack &s) /初始化s.base=(elemtype *)malloc(max_stacksize*s
11、izeof(elemtype);if(!s.base)exit(overflow);s.top=s.base;s.stacksize=max_stacksize;return ok;int push(sqstack &s,elemtype e) /入栈if(s.top-s.base>=max_stacksize)s.base=(elemtype *)realloc(s.base,(increase_size+s.stacksize)*sizeof(elemtype);if(!s.base)exit(overflow);s.top=s.base+s.stacksize;s.stac
12、ksize+=increase_size;*s.top+=e;return ok;int pop(sqstack &s,elemtype &e) /出栈if(s.top=s.base)return error;e=*-s.top;return ok;int gettop(sqstack &s,elemtype &e) /返回栈顶元素 if(s.top=s.base)return error;e=*(s.top-1);return ok;int stackempty(sqstack &s) /判断是否为空栈if(s.top=s.base)return TR
13、UE;elsereturn FALSE;int destroystack(sqstack &s) /销毁栈free(s.base);s.top=s.base;return ok;int pass(mazetype &maze,postype curpos) /当前位置未通过时if(maze.acurpos.rowcurpos.col='0')return TRUE;elsereturn FALSE;int footprint(mazetype &maze,postype curpos) /留下足迹maze.acurpos.rowcurpos.col=
14、39;*' /*表示可通的标记return TRUE;int markprint(mazetype &maze,postype curpos) /表示曾走过但不能走通的标记maze.acurpos.rowcurpos.col=''return TRUE;postype nextpos(postype &curpos,int i) /探索当前位置的下一个方向,东南西北分别表示为1234postype newcurpos;newcurpos=curpos;switch(i)case 1:newcurpos.col+=1;newcurpos.row=curpo
15、s.row;break; case 2:newcurpos.row+=1;newcurpos.col=curpos.col;break; case 3:newcurpos.col-=1;newcurpos.row=curpos.row;break;case 4:newcurpos.row-=1;newcurpos.col=curpos.col;break;default:exit(error);return newcurpos;void outputmaze(mazetype &maze)int i,j;printf(" ");for(i=0;i<=maze.
16、row+1;i+)printf("%4d",i);printf("n");for(i=0;i<=maze.row+1;i+)printf("%4d",i);for(j=0;j<=maze.col+1;j+)printf("%4c",maze.aij);printf("n");/printf("n");int mazepath(mazetype &maze,postype start,postype end) /求解迷宫路径函数sqstack s;int curstep=1;elemtype e;postype curpos; Initstack(s);curpos=start;doif(pass(maze,curpos)footprint(maze,curpos);e.ord=curstep; /e=(curstep,curpos,1);e.seat=curpos;e.di=1;push(s,e);if(curpos.row=end.row&&curpos.col=end.col)return (TRUE);printf("起点极为终点!n");destroys
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 事业单位服务合同管理流程
- 大型活动食堂餐饮服务租赁合同
- 万科物业租户合同管理协议2025
- 河南省洛阳市2024-2025学年高二下学期期中考试历史试卷(含答案)
- 2024-2025学年度江西省南昌中学(三经路校区)高一下学期期中考试历史试题(含答案)
- 江苏省淮安市2025届高三11月第一次调研测试-生物试题(含答案)
- 精细专利代理人合作协议
- 初中数学探索三角形全等的条件第1课时课件2024-2025学年北师大版数学七年级下册
- 第9课 辽、西夏与北宋并立 教学设计-2024-2025学年统编版(2024)七年级历史下册
- 英语Unit 3 This is Miss Li教案及反思
- 儿童早期综合发展课件
- 剪力墙平法识图讲义(PPT格式105)
- 专业工程分包业主审批表
- 药剂科终止妊娠药品管理制度
- 活动物料清单
- 08S305-小型潜水泵选用及安装图集
- 缺血缺氧性脑病详解课件
- 自动打铃控制器plc课程设计
- 最新司法鉴定程序通则课件来源于司法部司法鉴定局
- 门禁系统调试报告(共4页)
- 北师大版一年级英语下册期中测试卷
评论
0/150
提交评论