迷宫(C语言版).doc

上传人:豆**** 文档编号:28514752 上传时间:2022-07-28 格式:DOC 页数:28 大小:248.50KB
返回 下载 相关 举报
迷宫(C语言版).doc_第1页
第1页 / 共28页
迷宫(C语言版).doc_第2页
第2页 / 共28页
点击查看更多>>
资源描述

《迷宫(C语言版).doc》由会员分享,可在线阅读,更多相关《迷宫(C语言版).doc(28页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date迷宫(C语言版)/*注:本程序探索迷宫的优先顺序= 1-下、2-右、3-上、4-左 1-下、2-右、3-上、4-左 =总体趋势:下右,逆时针方向。因为出口就在右边下方*/#include#include#include#define stack_init_size 200#define stack_increment 10#define OVERFLOW 0#defi

2、ne OK 1#define ERROE 0#define TRUE 1#define FALSE 0typedef int Status;typedef structint x;int y;PosType;typedef struct int ord;/通道块在路径上的“序号”PosType seat;/通道块在迷宫中的“坐标位置”int di;/从此通道块走向下一通道块的“方向”SElemType;typedef structSElemType *base;SElemType *top;int stacksize;SqStack;int mg2020;/*随机生成迷宫的函数/*为了能够让尽

3、量能通过,将能通过的块和不能通过的块数量比大致为2:1*/void Random()int i,j,k;srand(time(NULL);mg10=mg11=mg1819=0;/将入口、出口设置为“0”即可通过for(j=0;j20;j+)mg0j=mg19j=1;/*设置迷宫外围“不可走”,保证只有一个出口和入口*/for(i=2;i19;i+)mgi0=mgi-119=1;/*设置迷宫外围“不可走”,保证只有一个出口和入口*/for(i=1;i19;i+)for(j=1;j=s.stacksize)s.base=(SElemType *)realloc(s.base,(s.stacksiz

4、e+stack_increment) *sizeof(SElemType);if(!s.base)exit(OVERFLOW);s.top=s.base+s.stacksize;s.stacksize+=stack_increment;*s.top+=e;return OK;/出栈Status Pop(SqStack &s,SElemType &e)if(s.top=s.base)return ERROE;e=*-s.top;return OK;/下一步PosType NextPos(PosType &e,int dir)PosType E;switch(dir)case 1:E.x=e.x;

5、/向下 E.y=e.y+1; break;case 2:E.x=e.x+1;/向右 E.y=e.y; break; case 3:E.x=e.x;/向上 E.y=e.y-1; break;case 4:E.x=e.x-1;/向左 E.y=e.y; break;return E;/是否空栈Status StackEmpty(SqStack s)if (s.top=s.base) return OK;return OVERFLOW; /留下不能通过的足迹Status MarkPrint(PosType e) mge.xe.y=3;return OK;/迷宫函数/ 若迷宫maze中从入口 start

6、到出口 end的通道,则求得一条存放在栈中/ (从栈底到栈顶),并返回TRUE;否则返回FALSEStatus MazePath(int mg,PosType start,PosType end,SqStack &s)PosType curpos;InitStack(s);SElemType e; int curstep;curpos=start;/ 设定当前位置为入口位置curstep=1;/ 探索第一步do if(Pass(curpos)/ 当前位置可通过,即是未曾走到过的通道块FootPrint(curpos);/ 留下足迹e.di =1; e.ord = curstep; e.seat

7、= curpos;Push(s,e);/ 加入路径if(curpos.x=end.x&curpos.y=end.y)printf(nn0_0 能到达终点!);return TRUE;curpos=NextPos(curpos,1);/ 下一位置是当前位置的东邻curstep+; / 探索下一步else/ 当前位置不能通过if(!StackEmpty(s)Pop(s,e);while(e.di=4&!StackEmpty(s)MarkPrint(e.seat);Pop(s,e);if(e.di4)e.di+;Push(s,e);/ 留下不能通过的标记,并退回一步curpos=NextPos(e.

8、seat,e.di);/* 当前位置设为新方向的相邻块*/if/if/elsewhile(!StackEmpty(s);printf(nn囧 ! 不能到达终点!);return FALSE;/打印迷宫void PrintMaze()int i,j;printf(运行路径:nn);for(i=0;i20;i+)for(j=0;j20;j+)if(mgij=0)printf( );else if(mgij=1) printf();/迷宫的“墙”else if(mgij=3) printf(); /不通的路else if(mgij=7)printf();/通过的路径 printf(n);printf

9、(n);void main()SqStack S;PosType start,end;start.x=1;start.y=0;/起点坐标end.x=18;end.y=19;/终点坐标printf(n=迷宫游戏=);printf(n说明:不能走的区域t走不通的区域);printf(n “空格”代表未到过的区域);printf(n 代表能通过的路径,指向终点);printf(n=);Random();printf(nnTest 1:); MazePath(mg2020,start,end,S); PrintMaze();system(pause);Random();printf(nTest 2:); MazePath(mg2020,start,end,S); PrintMaze();system(pause);Random();printf(nTest 3:); MazePath(mg2020,start,end,S); PrintMaze();printf(n=程序退出,感谢使用!=n);-

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com