




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#include #include #define overflow -1#define ok 1#define error 0#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int Status;typedef struct int x;int y;PosType;/位置类型typedef structint row,col;int a5050;MazeType;/迷宫类型typedef structint ord;/通道块在路径上的序号PosType seat;/通道块在迷宫中的“坐标位置”int di;/从此通道块走向下一个通道块的方向SElemType;/栈的元素类型typedef structSElemType *base;SElemType *top;int Stacksize;SqStack;/栈结构Status InitStack(SqStack &S)/初始化栈S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType);if(!S.base) exit (overflow);S.top=S.base;S.Stacksize=STACK_INIT_SIZE;return ok;Status Push(SqStack &S,SElemType e)/入栈if(S.top-S.base=S.Stacksize)S.base=(SElemType *)realloc(S.base,(S.Stacksize+STACKINCREMENT)*sizeof(SElemType); if(!S.base) exit (overflow);S.top=S.base+S.Stacksize;S.Stacksize+=STACKINCREMENT;*S.top+=e;return ok;Status Pop(SqStack &S,SElemType &e)/出栈if(S.top=S.base) return error;e=*-S.top;return ok;bool StackEmpty(SqStack S)/判断栈是否为空if(S.top=S.base)return ok;else return error;Status InitMaze(MazeType &maze)/初始化迷宫 int i,j; for( j=0;jmaze.col+2;j+)maze.a0j=1;for( i=1;imaze.row+1;i+)maze.ai0=1;maze.aimaze.col+1=1; for(int j=1;jmaze.col+1;j+) scanf(%d,&maze.aij); for(j=0;jmaze.col+2;j+)maze.amaze.row+1j=1; return ok;/为了避免检查边界,把迷宫的外围都设成障碍,迷宫的内核是row行,col列的数组bool Pass(MazeType maze,PosType curpos)/判断是否可以通过return maze.acurpos.xcurpos.y=0;Status FootPrint(MazeType &maze,PosType curpos)/留下足迹 maze.acurpos.xcurpos.y=2; return ok;SElemType CreatElem(int step,PosType pos,int di)/创造一个SElemType型的数据SElemType e;e.ord=step;e.seat=pos;e.di=di;return e;bool IsEnd(PosType pos1,PosType pos2)/判断是否结束return pos1.x=pos2.x&pos1.y=pos2.y;PosType NextPos(PosType curpos,int di)/向下一步搜索 PosType pos=curpos; switch(di) case 1:pos.x+;break;/向东case 2:pos.y+;break;/向南case 3:pos.x-;break;/向西case 4:pos.y-;break;/向北 return pos;Status MarkPrint(MazeType &maze,PosType curpos)/在死路处留下印迹 maze.acurpos.xcurpos.y=*; return ok;Status MazePath(MazeType &maze,PosType start,PosType end)/寻找路径/若迷宫maze中存在从入口start到出口end的通道,则求得一条存放在栈中(从栈底到栈顶),/并返回ture;否则返回falseSqStack S;SElemType e;InitStack(S);PosType curpos=start;/设定当前位置为入口位置int curstep=1;/探索第一步doif(Pass(maze,curpos)FootPrint(maze,curpos);/留下足迹e=CreatElem(curstep,curpos,1);Push(S,e);/加入路径if(IsEnd(curpos,end)return ok;/达到终点curpos=NextPos(curpos,1);/下一步是当期位置的东邻curstep+;/探索下一步else/当前位置不能通过if(!StackEmpty(S)Pop(S,e);while(e.di=4&!StackEmpty(S)MarkPrint(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;Status PrintMaze(MazeType maze)/输出迷宫 int i,j; for(i=0;imaze.row+2;i+) for(j=0;jmaze.col+2;j+) printf(%d ,maze.aij); printf(n); return ok; void main() MazeType maze;printf(请输入迷宫的行数row和列数col:);scanf(%d %d,&maze.row,&maze.col);printf(请输入迷宫数据,0表示通道,1表示障碍:n);InitMaze(maze);PosType star
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 ISO/IEC 11770-3:2021/Amd 1:2025 EN Information security - Key management - Part 3: Mechanisms using asymmetric techniques - Amendment 1: TFNS identity-based key agreement
- 【正版授权】 IEC 61000-6-2:2005 EN-D Electromagnetic compatibility (EMC) - Part 6-2: Generic standards - Immunity for industrial environments
- 校园应急知识培训课件简报
- 造价方面考试试题及答案
- 浙江杭州面试题及答案
- 回乡创业考试题库及答案
- 语文开卷考试试题及答案
- 校园安全知识培训心得
- 粤电集团入职考试试题及答案
- 行政人员考试试题及答案
- 2025年江苏省苏豪控股集团有限公司校园招聘笔试备考试题及答案详解(必刷)
- (完整)中小学“学宪法、讲宪法”知识竞赛题库及答案
- 2025年行政执法人员执法证考试必考多选题库及答案(共300题)
- 垃圾分类巡检督导方案
- 乳制品配送服务应急处理方案
- 公司收款授权委托书标准
- 健康中国行动心理健康促进行动
- 小儿呼吸系统生理特点解剖护理课件
- 中音萨克斯名曲经典十首
- 2016室性早搏治疗指南
- 数控折弯机简易数控系统SNC说明书操作手册
评论
0/150
提交评论