欢迎来到天天文库
浏览记录
ID:47518187
大小:41.50 KB
页数:7页
时间:2020-01-12
《数据结构迷宫源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#include#defineOK1#defineERROR0#defineNULL0#defineOVERFLOW-2#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10//栈的顺序存储表示typedefstruct{intx;/*列*/inty;/*行*/}PosType;//坐标位置类型typedefstruct{intord;//通道块在路径上的序
2、号PosTypeseat;//通道块在迷宫中的坐标位置intdi;//从此通道块走向下一通道块的方向}SElemType;//栈的元素类型typedefstruct{SElemType*base;SElemType*top;intstacksize;//当前已分配的存储空间,以元素为单位}SqStack;//基本操作intInitStack(SqStack*S){S->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!S->base)exit(OVERFLOW);S->top=S
3、->base;S->stacksize=STACK_INIT_SIZE;returnOK;}//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERRORintGetTop(SqStack*S,SElemType*e){if(S->top==S->base)returnERROR;*e=*(S->top-1);returnOK;}intPush(SqStack*S,SElemType*e)//插入元素e作为新的栈顶元素{if(S->top-S->base>=S->stacksize)/*栈满,追加存储空间*/{S->base=(SElemType*
4、)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;returnOK;}//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERRORintPop(SqStack*S,SElemType*e){if(S->top==S->base)returnERROR;*e=*--
5、S->top;returnOK;}intStackEmpty(SqStack*S){return(S->top==S->base);}//迷宫程序typedefstruct{intlie;/*列数*/inthang;/*行数*/chara[999][999];}MazeType;/*迷宫类型*//*随机生成迷宫*/intgeneratemaze(MazeType*maze){inti,j;maze->a[0][0]=2;maze->a[++maze->hang][++maze->lie]=3;/*设置外墙*/maze->a[0][maze->lie]='
6、!';maze->a[maze->hang][0]='!';for(j=1;jlie;j++){maze->a[0][j]='!';maze->a[maze->hang][j]='!';}for(i=1;ihang;i++){maze->a[i][0]='!';maze->a[i][maze->lie]='!';}srand((unsigned)time(NULL));rand();for(i=1;ihang;i++)for(j=1;jlie;j++){if(rand()>=RAND_MAX/4)m
7、aze->a[i][j]='';//''暗示出路elsemaze->a[i][j]='!';//'!'暗示无出路}returnOK;}intPass(MazeType*maze,PosTypecurpos)//判断当前位置可否通过{if((curpos.x<1)
8、
9、(curpos.x>=maze->lie))returnERROR;if((curpos.y<1)
10、
11、(curpos.y>=maze->hang))returnERROR;if(maze->a[curpos.y][curpos.x]=='')returnOK;elsereturnERROR;}
12、intFootPrint(MazeType*maze,PosTypecurpos
此文档下载收益归作者所有