版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、迷宫最短路径问题描述 从一个迷宫的入口到出口找出一条最短路经。用一个二维数组MAZE(1:m,1:n)模拟迷宫,数组元素为0表示该位置可以通过, 数组元素为1表示该位置不可以通行。MAZE(1,1)和MAZE(m,n)分别为迷宫的入口和出口。基本要求输入数据输入迷宫的大小m行和n列,两者为整数由随机数产生0或1,建立迷宫。输出数据首先输出模拟迷宫的二维数组,若存在最短路经,则由出口回朔到入口打印这一条路径,如下所示: (m,n), , (I,j), , (1,1)如无通道,则打印: THERE IS NO PATH.#include#define OVERFLOW -2#define ERRO
2、R 0#define NULL 0#define true 1#define TRUE 1#define false 0#define FALSE 0#define STACK_INIT_SIZE 100#define STACKINCREMENT 10#include #include /*初始化迷宫,1表示通道,0表示墙*/typedef struct MStackElem int x; int y; int val;MStackElem;typedef struct MStackElem * base; MStackElem * top; int stackSize;MStack;voi
3、d initStack(MStack *s) s-base = (MStackElem *)malloc(STACK_INIT_SIZE * sizeof(MStackElem); if (!s-base) printf(in initStack().Failed to initalize the MStack ,no enough space! exit now. ); exit(OVERFLOW); s-top = s-base; s-stackSize = STACK_INIT_SIZE;void push(MStack *s,MStackElem e) if (s-top - s-ba
4、se = s-stackSize) s-base = (MStackElem *)realloc(s-base, (STACK_INIT_SIZE+STACKINCREMENT) * sizeof(MStackElem); if (!s-base) printf(in push().Failed to realloc the MStack ,no enough space! exit now. ); exit(OVERFLOW); s-top = s-base + s-stackSize; s-stackSize += STACKINCREMENT; *(s-top+) = e;MStackE
5、lem getTop(MStack *s) if (s-top = s-base) printf(in getTop(),empty stack! exit now. ); exit(ERROR);else return *(s-top - 1);void pop(MStack *s) if (s-top = s-base) printf(in pop(),empty stack! exit now. ); exit(ERROR); else -(s-top); MStack realPath,path;int unPass(MStack path,MStackElem cur) int fl
6、ag = 1; while(path.top != path.base) MStackElem e = *(path.top - 1); if (e.x = cur.x& e.y = cur.y)HYPERLINK /p/696325105#回复2楼2010-01-15 19:07HYPERLINK /p/696325105#举报 |吧友125.77.120.* flag = 0; (path.top)-; return flag;MStackElem getEast(MStackElem cur,int *maze,int n) if(cur.y != 7) cur.y += 1; cur.
7、val = *(maze+cur.x*n+cur.y); return cur; MStackElem getSouth(MStackElem cur,int *maze,int n) if(cur.x != 7) cur.x += 1; cur.val = *(maze+cur.x*n+cur.y); return cur;MStackElem getWest(MStackElem cur,int *maze,int n) if(cur.y != 0) cur.y -= 1; cur.val = *(maze+cur.x*n+cur.y); return cur;MStackElem get
8、North(MStackElem cur,int *maze,int n) if(cur.x != 0) cur.x -= 1; cur.val = *(maze+cur.x*n+cur.y); return cur;MStackElem getNext(MStackElem cur,int *maze,int n) MStackElem next;next.x = next.y=next.val = -1;if(getEast(cur,*maze,n).val != 0 & unPass(path,getEast(cur,*maze,n) next = getEast(cur,*maze,n
9、);else if(getSouth(cur,*maze,n).val != 0 & unPass(path,getSouth(cur,*maze,n) next = getSouth(cur,*maze,n);else if(getWest(cur,*maze,n).val != 0 & unPass(path,getWest(cur,*maze,n) next = getWest(cur,*maze,n);else if(getNorth(cur,*maze,n).val != 0 & unPass(path,getNorth(cur,*maze,n) next = getNorth(cu
10、r,*maze,n);return next;int getMazePath(int *maze,int n)HYPERLINK /p/696325105#回复3楼2010-01-15 19:07HYPERLINK /p/696325105#举报 |吧友125.77.120.* MStackElem start,end,cur; start.x = 0; start.y = 0; start.val = *(maze+start.x*n+start.y); end.x = 7; end.y = 7; end.val = *(maze+end.x*n+end.y); cur = start; printf(%d,cur.x); printf(%d,cur.y); printf(%d,cur.val); do if (unPass(path,cur) push(&realPath,cur); push(&path,cur); cur = getNext(cur,*maze,n); if (cur.x = end.x & cur.y = end.y) push(&realPath,cur); push(&path,cur); return true; else if(cur.val = -1) po
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025江西省井冈山应用科技学校工作人员招聘考试试题
- 2025江苏省车辐中等专业学校工作人员招聘考试试题
- 大学校区做电气预防性试验的施工组织设计
- 初中历史教学中数字博物馆虚拟游览课程设计课题报告教学研究课题报告
- 高中生借助话语分析考察20世纪诗歌批评话语的演变特征课题报告教学研究课题报告
- 2026年5G通信网络切片技术报告及未来五至十年应用拓展报告
- 危大工程施工方案-土方开挖工程
- 2025年微生物降解行业竞争分析报告
- G网络下高中智慧校园智能学习环境中的学生自主学习策略研究教学研究课题报告
- 2026年物流业智能仓储系统分析报告
- 2026年少先队考核模拟试题及答案详解(全优)
- 中国金谷国际信托有限责任公司招聘笔试备考试题及答案解析
- 小学一年级语文下册《荷叶圆圆》跨学科融合教学设计(导学案)
- 湖南 2026 政府采购评审专家续聘考试(3) 真题
- 2026天津富凯建设集团有限公司招聘工作人员招聘4人考试参考题库及答案解析
- 2025年芯片测试岗笔试题目及答案
- 预应力混凝土空心方桩08SG360
- 安宁疗护病区工作制度
- 2026年上海市杨浦区中考数学二模试卷(含解析)
- 电梯施工临时用电安全方案
- 亚克力生产车间安全讲解
评论
0/150
提交评论