c语言迷宫最完整做法

上传人:m**** 文档编号:498019598 上传时间:2023-01-23 格式:DOC 页数:6 大小:82.50KB
返回 下载 相关 举报
c语言迷宫最完整做法_第1页
第1页 / 共6页
c语言迷宫最完整做法_第2页
第2页 / 共6页
c语言迷宫最完整做法_第3页
第3页 / 共6页
c语言迷宫最完整做法_第4页
第4页 / 共6页
c语言迷宫最完整做法_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《c语言迷宫最完整做法》由会员分享,可在线阅读,更多相关《c语言迷宫最完整做法(6页珍藏版)》请在金锄头文库上搜索。

1、#includestdio。hincludestdlib。hincludedefine stack_init_size 200define stack_increment 10define ERROE 0#define TRUE 1define FALSE 0#define OVERFLOW 0#define OK 1typedef int Status;typedef structint x;int y;PosType;typedef struct int ord;/通道块在路径上的“序号”PosType seat;/通道块在迷宫中的“坐标位置”int di;/从此通道块走向下一通道块的“方

2、向SElemType;typedef structSElemType base;SElemType top;int stacksize;SqStack;int mg2020;/随机生成迷宫的函数/为了能够让尽量能通过,将能通过的块和不能通过的块数量比大致为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=mgi119=1;/设置迷宫外围“

3、不可走,保证只有一个出口和入口/for(i=1;i19;i+)for(j=1;j=s。stacksize)s。base=(SElemType *)realloc(s。base,(s.stacksize+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 StackEmpty(SqStack s)if (s。top=s.base) return OK;ret

4、urn OVERFLOW; /留下不能通过的足迹Status MarkPrint(PosType e) mge。xe。y=3;return OK;/下一步PosType NextPos(PosType &e,int dir)PosType E;switch(dir)case 1:E.x=e。x;/向下 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。y1; break;case 4:E.x=e.x-1;/向左 E.y=e。y; break;return E;/ 若迷宫maze中从入口

5、start到出口 end的通道,则求得一条存放在栈中/ (从栈底到栈顶),并返回TRUE;否则返回FALSE/迷宫函数Status 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 = curs

6、tep; e。seat= curpos;Push(s,e);/ 加入路径if(curpos。x=end.x curpos。y=end.y)printf(nn0_0 能到达终点!n路径如下:n”);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);/ 留下不能通过的标记,并退

7、回一步curpos=NextPos(e。seat,e.di);/* 当前位置设为新方向的相邻块/if/if/elsewhile(!StackEmpty(s);printf(”nn此迷宫不能走通!n);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(

8、”);/通过的路径 printf(”n);printf(”n);void main()SqStack S;PosType start,end;start。x=1;start.y=0;/起点坐标end。x=18;end。y=19;/终点坐标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();

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

当前位置:首页 > 机械/制造/汽车 > 电气技术

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号