迷宫求解

 

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<conio.h>
#define OK 1
#define FASLE 0
#define ERROR -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
 //int x;
 //int y;
 int gtflag;
 int ftflag;
 int mkflag;
}PosType;

typedef struct
{
 int x;
 int y;
}Markxy;
Markxy curpos,start,end;

typedef struct
{
 int ord;
 Markxy seat;
 int di;
}SElemType;
SElemType e;

typedef struct
{
 PosType mzptr[10][10];
 int length;
 int width;
}MazeType;
MazeType maze;

typedef struct
{
 SElemType *base;
 SElemType *top;
 int stacksize;
}SqStack;
SqStack S;

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;
}

int 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;
}

int Pop(SqStack &S,SElemType &e)
{
 if(S.top == S.base)
  return ERROR;
 e = *--S.top;
 return OK;
}

int StackEmpty(SqStack &S)
{
 if(S.top==S.base)
  return OK;
 return FASLE;
}

int Pass(Markxy &pos)
{
 int i=pos.x;
 int j=pos.y;
 if((maze.mzptr[i][j].gtflag==1)||(maze.mzptr[i][j].ftflag==1)||(maze.mzptr[i][j].mkflag==1))
  return FASLE;
 return OK;
}

void FootPrint(Markxy &pos)
{  
 int i=pos.x;
 int j=pos.y;
 maze.mzptr[i][j].ftflag=1;
}

void MarkPrint(Markxy &pos)
{
 int i=pos.x;
 int j=pos.y;
 maze.mzptr[i][j].mkflag=1;
}

int NextPos(Markxy pos,int dir)
{

 if(dir==1)
 {
  curpos.y=pos.y+1;
  curpos.x=pos.x;
 }
 else if(dir==2)
 {
  curpos.x=pos.x+1;
  curpos.y=pos.y;
 }
 else if(dir==3)
 {
  curpos.y=pos.y-1;
  curpos.x=pos.x;
 }
  else
  {
   curpos.x=pos.x-1;
   curpos.y=pos.y;
  }
 return OK;
}

int MazePath(MazeType maze,Markxy start,Markxy end)
{
 InitStack(S);
 curpos.x=start.x;
 curpos.y=start.y;
    int curstep=1;
 do
 {
  if(Pass(curpos))
  {
   FootPrint(curpos);
   e.ord=curstep;
   e.seat.x=curpos.x;
   e.seat.y=curpos.y;
   e.di=1;
   Push(S,e);
   if((curpos.x==end.x)&&(curpos.y==end.y))
    return OK;
      NextPos(curpos,1);
   curstep++;
  } 
  else
  {
   if(!StackEmpty(S))
   {
    Pop(S,e);
    while(e.di==4&&!StackEmpty(S))
    {
     MarkPrint(e.seat);
     Pop(S,e);
     curstep--;
    }
    if(e.di<4)
    {
     e.di++;
     Push(S,e);
     NextPos(e.seat,e.di);
    }
   }
  }
  }while(!StackEmpty(S));
return FASLE;
}

int main()
{
    int i,j;
 maze.length=10;
 maze.width=10;
 for(i=0;i<10;i++)
  for(j=0;j<10;j++)
  {
   maze.mzptr[i][j].gtflag=0;
   maze.mzptr[i][j].ftflag=0;
   maze.mzptr[i][j].mkflag=0;   
  }
 for(j=0;j<10;j++)
 {
  maze.mzptr[0][j].gtflag=1;
  maze.mzptr[9][j].gtflag=1;
  
 }
 for(i=0;i<10;i++)
 {
  maze.mzptr[i][0].gtflag=1;
  maze.mzptr[i][9].gtflag=1;
 }
  maze.mzptr[1][3].gtflag=1;
  maze.mzptr[1][7].gtflag=1;
     maze.mzptr[2][3].gtflag=1;
  maze.mzptr[2][7].gtflag=1;  
  maze.mzptr[3][5].gtflag=1;
  maze.mzptr[3][6].gtflag=1;
  maze.mzptr[4][2].gtflag=1;  
  maze.mzptr[4][3].gtflag=1;
  maze.mzptr[4][4].gtflag=1;
     maze.mzptr[5][4].gtflag=1;
     maze.mzptr[6][2].gtflag=1;
  maze.mzptr[6][6].gtflag=1;
  maze.mzptr[7][2].gtflag=1;
     maze.mzptr[7][3].gtflag=1;
      //maze.mzptr[7][5].gtflag=1;
  maze.mzptr[7][4].gtflag=1;
     maze.mzptr[7][6].gtflag=1;
  maze.mzptr[7][7].gtflag=1;
  maze.mzptr[8][1].gtflag=1;
 start.x=1;
 start.y=1;
 end.x=8;
 end.y=8;
 MazePath(maze,start,end);
 //printf("%d,%d\n",S.base,S.top);
 while(S.base!=S.top)
 {
 e=*--S.top;
 printf("这是第%d步:\n",e.ord);
 if(e.di==1)
  printf("向右走\n");
 else if(e.di==2)
  printf("向下走\n");
 else if(e.di==3)
  printf("向左走\n");
 else
  printf("向上走\n");
 printf("%d,%d\n",e.seat.x,e.seat.y);
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值