数据结构-迷宫解算法.docVIP

  • 3
  • 0
  • 约 6页
  • 2016-10-17 发布于贵州
  • 举报
数据结构-迷宫解算法

#include stdio.h #include stdlib.h #define overflow -1 #define ok 1 #define error 0 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int Status; typedef struct { int x; int y; }PosType;//位置类型 typedef struct { int row,col; int a[50][50]; }MazeType;//迷宫类型 typedef struct { int ord;//通道块在路径上的序号 PosType seat;//通道块在迷宫中的“坐标位置” int di;//从此通道块走向下一个通道块的方向 }SElemType;//栈的元素类型 typedef struct { SElemType *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.a[0][j]=1; for( i=1;imaze.row+1;i++) { maze.a[i][0]=1; maze.a[i][maze.col+1]=1; for(int j=1;jmaze.col+1;j++) scanf(%d,maze.a[i][j]); } for(j=0;jmaze.col+2;j++) maze.a[maze.row+1][j]=1; return ok; }//为了避免检查边界,把迷宫的外围都设成障碍,迷宫的内核是row行,col列的数组 bool Pass(MazeType maze,PosType curpos)//判断是否可以通过 { return maze.a[curpos.x][curpos.y]==0; } Status FootPrint(MazeType maze,PosType curpos)//留下足迹 { maze.a[curpos.x][curpos.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.xpos1.y==pos2.y; } PosType NextPos(PosType curpos,int

文档评论(0)

1亿VIP精品文档

相关文档