C迷宫源代码

转自:http://blog.csdn.net/qq352773277/article/details/8209513

#include 
#include 
#include 


#define M 20
#define N 20

#define visited 2
#define TRUE 1
#define FALSE 0
#define INITSIZE 100

typedef int Status;

typedef struct{       //坐标点结构体
 int y;    //每个可通的行坐标
 int x;  //每个可通的列坐标
}PosType;

typedef struct{
 int ord;     //通道块在路径上的"序号"
 int di;    //从此通道块走向下一通道块的"方向"
 PosType seat;    //通道块在迷宫中的"坐标位置"
}MazeNode;     //迷宫节点

typedef struct {

 MazeNode base[INITSIZE];
 int top;   //栈顶指针

}Stack;

typedef struct{                 //用于存储迷宫的路径
 PosType coor[INITSIZE];
 int top;
}Postion;


void RandMatrix();     //随机生成迷宫
int InitStack(Stack *);   //初始化栈
int InitStack1(Postion *);
int StackEmpty(Stack *);    //判断栈是否为空
int StackEmpty1(Postion *);
int StackIsFull(Stack *);  //判断栈是否满了
int StackIsFull1(Postion *);  //判断栈是否满了

int Push(Stack *s,MazeNode m);    //压栈
int Push1(Postion *,PosType);
int Pop(Stack *s,MazeNode *m);  //出栈
int Pop1(Postion *,PosType *);
int DestroyStack(Stack *s);   //撤销栈
int Pass(PosType pos); //判断指定坐标是否可通过

int FootPrint(PosType pos);      //标记能通过的
PosType NextCoord(PosType pos,int i);  //获取下一位置

int MarkPrint(PosType pos);     //留下不能通过的标记,并退回一步
int MazePath(PosType start,PosType end,Postion *);   //从迷宫的入口到出口查找
void PrintMaze(Postion *);   //输出迷宫

int mg[M][N];    //生成一个M*N 的迷宫

int main()
{
 int h=1;
 PosType start,end;
 Postion P;
 while(h)
 {
 printf("创建迷宫\n");
 InitStack1(&P);
 RandMatrix();

 printf("\n");
 printf("1、重新生成迷宫,0、就这个:");
 scanf("%d",&h);
 }
 do   //输入迷宫入口坐标
 {
  printf("\n输入迷宫入口坐标");
  scanf("%d%d",&start.x,&start.y);
  if(start.x>N || start.y>M)
  {
   printf("输入的坐标越界,请重新输入!\n");
   continue;
  }
 }while(start.x>N || start.y>M);
 do   //输入迷宫出口坐标
 {
  printf("\n输入迷宫出口坐标:");
  scanf("%d%d",&end.x,&end.y);
  if(end.x>N || end.y>M)
  {
   printf("输入的坐标越界,请重新输入!\n");
   continue;
  }
 }while(end.x>N || end.y>M);
 if(!MazePath(start,end,&P))    //调用函数查找路径
 {
  printf("\n无法通过!\n");
 }
 else
 {
  PrintMaze(&P);    //打印找到的路径
 } 
    system("pause"); 
 return 0;

}
int InitStack(Stack *s)
{
 s->top=-1;
 return 1;
}
int InitStack1(Postion *s)
{
 s->top=-1;
 return 1;
}
int StackEmpty(Stack *s)
{
 if(s->top==-1)
  return 1;
 return 0;
}
int StackEmpty1(Postion *s)
{
 if(s->top==-1)
  return 1;
 return 0;
}
int StackIsFull(Stack *s)
{
 if(s->top==INITSIZE-1)
  return 1;
 else
  return 0;
}
int StackIsFull1(Postion *s)
{
 if(s->top==INITSIZE-1)
  return 1;
 else
  return 0;
}

int Push(Stack *s,MazeNode m)
{
 if(!StackIsFull(s))
 {
  s->top++;
  s->base[s->top]=m;
 }
 return 1;
}
int Push1(Postion *s,PosType m)
{
 if(!StackIsFull1(s))
 {
  s->top++;
  s->coor[s->top]=m;
 }
 return 1;
}

int Pop(Stack *s,MazeNode *m)
{
 if(s->top!=-1)
 {
  *m=s->base[s->top];
  s->top--;
  return 1;
 }
 return 1;
}
int Pop1(Postion *s,PosType *m)
{
 if(s->top!=-1)
 {
  *m=s->coor[s->top];
  s->top--;
  return 1;
 }
 return 1;
}
int DestroyStack(Stack *s)
{
 s->top=-1;
 return 1;
}
int Pass(PosType pos) //判断指定坐标是否可通过
{
 if(mg[pos.y][pos.x]==0)  //可通
  return 1;
 else
  return 0;
}

int FootPrint(PosType pos)   //标记能通过的
{
 mg[pos.y][pos.x]=2;      //2表示可通
 return 1;
}

PosType NextCoord(PosType pos,int i)  //获取下一位置
{
 switch(i)   //1,2,3,4,5,6,7,8代表方向顺时针
 {
  case 1:
   pos.x+=1;  //向右侧查找
   break;
  case 2:
   pos.x+=1;
   pos.y+=1;
   break;
  case 3:
   pos.y+=1;
   break;
  case 4: 
   pos.y+=1;
   pos.x-=1;
   break;
  case 5:
   pos.x-=1;
   break;
  case 6:
   pos.x-=1;
   pos.y-=1;
   break;
  case 7:
   pos.y-=1;
   break;
  case 8:
   pos.y-=1;
   pos.x+=1;
   break;
  default :
   exit(0);
 }
 return pos;
}


int MarkPrint(PosType pos)   //留下不能通过的标记,并退回一步
{
 mg[pos.y][pos.x]=3;   //3表示曾走过,但不通
 return 1;
}

void RandMatrix()
{
 int i=0,j=0;
 srand((unsigned)time(NULL));
 for(i=0;i


 

你可能感兴趣的:(小程序,c++,C++)