数据结构迷宫源代码.docxVIP

  • 16
  • 0
  • 约4.51千字
  • 约 13页
  • 2020-03-26 发布于江西
  • 举报
#include stdio.h #include malloc.h #include stdlib.h #include string.h #include time.h #define OK 1 #define ERROR 0 #define NULL 0 #define OVERFLOW -2 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 //栈的顺序存储表示 typedef struct{ int x; /*列*/ int y; /*行*/ }PosType; //坐标位置类型 typedef struct{ int ord; //通道块在路径上的序号 PosType seat; //通道块在迷宫中的坐标位置 int di; //从此通道块走向下一通道块的方向 }SElemType; //栈的元素类型 typedef struct { SElemType *base; SElemType *top; int stacksize; //当前已分配的存储空间,以元素为单位 }SqStack; //基本操作 int 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; } //若栈不空,则用 e 返回 S 的栈顶元素,并返回 OK;否则返回 ERROR int GetTop(SqStack *S,SElemType *e) { if(S-top==S-base) return ERROR; *e=*(S-top-1); return OK; } int Push(SqStack *S,SElemType *e)//插入元素 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; } //若栈不空,则删除 S 的栈顶元素,用 e 返回其值,并返回 OK;否则返回 ERROR int Pop(SqStack *S,SElemType *e) { if(S-top==S-base) return ERROR; *e=*--S-top; return OK; } int StackEmpty(SqStack *S) { return(S-top==S-base) ; } //迷宫程序 typedef struct { int lie; /*列数*/ int hang; /*行数*/ char a[999][999]; }MazeType; /*迷宫类型*/ /*随机生成迷宫*/ int generatemaze( MazeType *maze) { int i,j; maze-a[0][0]=2; maze-a[++maze-hang][++maze-lie]=3; /*设置外墙*/ maze-a[0][maze-lie]=!; maze-a[maze-hang][0]=!; for(j=1;jmaze-lie;j++) {maze-a[0][j]=!;maze-a[maze-hang][j]=!;} for(i=1;imaze-hang;i++) {maze-a[i][0]=!;maze-a[i][maze-lie]=!;} srand((unsigned)time( NULL )); rand(); for(i=1; i maze-hang; i++) for(j=1;jmaze-lie;j++) { if (rand()=RAND_MAX/4) maze-a[i][j] = ; // 暗示出路 else maze-a[i][j] =!; //!暗示无出路 } return OK; } int Pass(MazeType *maze, PosType curpos ) //判断当前位置可否通过 { if ((curpos.x 1) || (curpos.x = maze-lie)) return ERROR; if ((curpo

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档