迷宫求解(数据结构)

#include"stdio.h"
#include"malloc.h"
#define MAX 10
#define L 10
#define C 10

int sum[L][C];
typedef struct
{
 int i;
    int j;
}postype;

typedef struct
 {
 int ort;
 postype seat;
 int di;
 }selemtype;
typedef struct
{
 selemtype *base;
    selemtype *top;
    int sizemax;
 
}stack;

 int initstack(stack *st)
 {
  st->base=(selemtype *)malloc(MAX*sizeof(selemtype));
  if(!st->base) return 0;
  st->top=st->base ;
  st->sizemax=MAX;
    return 1;
 }
int empty(stack *st)
{
 if(st->top-st->base>=st->sizemax)
      return 0;
   else if(st->base==st->top)
     return 1;
   return 3;
}
int push(stack *st,selemtype *e)
{
 if(empty(st)==0)
 {
  st->base =(selemtype *)realloc(st->base,(st->sizemax+3)*sizeof(selemtype));
     if(!st->base) return 0;
  st->sizemax +=3;
 }
 *(st->top)=*e;
 st->top++;

   return 1;
   
}
int pop(stack *st,selemtype *e)
{
 if(empty(st)==1)
  return 0;
  *e=*(st->top-1);
  st->top--;
  return 1;

}
 void print(stack *outst)
 {
   selemtype post1;
   stack outst1;
   initstack(&outst1);
   while(true)
   {
    if(pop(outst,&post1)==1)
     push(&outst1,&post1);
 else break;
   }

   while(true)
   {
    if(pop(&outst1,&post1)==1)
    printf("%d,%d,%d,%d/n",post1.ort,post1.seat.i,post1.seat.j,post1.di);
    else break;
   }

 }

 void initsum()
 {
  int i=0,j=0;
  for(i=0;i<L;i++)
   for(j=0;j<C;j++)
    sum[i][j]=0;
  for(j=0,i=0;j<10;j++) sum[i][j]=1;
     for(i=9,j=0;j<10;j++) sum[i][j]=1;
     for(j=0,i=0;i<10;i++) sum[i][j]=1;
  for(j=9,i=0;i<10;i++) sum[i][j]=1;
     
   for(i=1,j=3;i<3;i++)sum[i][j]=1;
   for(i=1,j=7;i<3;i++) sum[i][j]=1;
   sum[4][2]=1;sum[4][3]=1;sum[4][4]=1;
   sum[3][5]=1;sum[3][6]=1;sum[5][4]=1;
   sum[6][2]=1;sum[6][6]=1;
   for(i=7,j=2;j<5;j++) sum[i][j]=1;
      sum[7][6]=1;sum[7][7]=1;sum[8][1]=1;
   sum[1][0]=0;sum[8][9]=8;

 }
void main()
{
   int di=0,i=1,j=0,n;
   int a=1,b=0;
   stack outst;
 
   selemtype post;
   post.ort =0;
   initsum();
   initstack(&outst);
  while(true)
  {
   a=i;b=j;
   di=0;
   if(sum[i][j]==8){ printf("按下路径走,你能成功出来了:/n");print(&outst);break;}
    while(di<4)
    {     a=i;b=j;
    if(di==0) b=j+1;
    if(di==1) a=i+1;
    if((di==2)&&(j!=0))b=j-1;
    if(di==3) a=i-1;
    if((sum[a][b]==1)||(sum[a][b]==2)||(sum[a][b]==3))
    {
                  di++;
    }
      else
      { 
    sum[i][j]=2;
          post.di=di;
          post.ort++;
          post.seat.i=i;
          post.seat.j=j;
          push(&outst,&post);
                if(di==0) j++;
             if(di==1) i++;
             if(di==2) j--;
             if(di==3) i--;
                 break;
      }
    }

 
   if(di>=4)
   {
    sum[i][j]=3;
       n=pop(&outst,&post);
    if(n==1)
    {
      i=post.seat.i ;
      j=post.seat.j;
    }
    else
    {
     printf("迷宫无法出去,是个死宫!");
     break;
    }

    
   }
  }

}

 

 

 

 

 

 

/* ****迷宫算法求解************* */ /**计目标:教学演示**************/ #include #define rows 10 #define cols 10 typedef struct {int row; int col; }PosType;/* //坐标点结构 */ typedef struct {int ord;/*//通道块在路径上的“序号” */ PosType seat;/*//通道块在迷宫中的“坐标位置”*/ int di;/*//从此通道快走向下一通道块的“方向” */ }SElemType;/*//栈的元素类型 */ typedef struct {SElemType *base; SElemType *top; int stacksize; }SqStack;/*//堆栈结构 */ void InitStack(SqStack &s)/*//初始化堆栈 */ { s.base=(SElemType *)malloc(100*sizeof(SElemType)); if(!s.base) return NULL; s.top=s.base; s.stacksize=100; } int StackEmpty(SqStack s)/* //栈空判别*/ {return(s.top==s.base); } void Pop(SqStack &s ,SelemType &e)/*//弹栈 */ {e=*--s.top); } void Push(SqStack &s,SElemType e)/*//将元素压入堆栈*/ { *s.top++=e; } /*static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,0,1,0}, {0,1,1,0,1,0,0,1,1,0}, {0,1,1,0,0,1,1,1,1,0}, {0,1,1,1,0,1,1,1,1,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,0,0,1,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; */ /* //初始迷宫数据(1-通,0-不通)*/ static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,1,1,0}, {0,1,1,1,0,0,0,0,1,0}, {0,1,0,0,0,1,1,1,1,0}, {0,1,0,1,0,1,0,0,0,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,1,0,0,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; /* //初始迷宫数据(1-通,0-不通)*/ static int foot[10][10]={0};/*//标记某点是否走过(1-走过,0-未走过)*/ void printpath(SqStack &s)/*//打印迷宫通路*/ {int i,j; SElemType e; while(!StackEmpty(s)) { Pop(s,e); foot[e.seat.row][e.seat.col]=1; } for(i=0;i<10;i++) {printf("\n"); for(j=0;j<10;j++) if(foot[i][j]) printf(" # "); else printf(" . "); } } int Pass(PosType pos)/*//判断当前的通道块是否可通*/ { return(maze[pos.row][pos.col]); }; void FootPrint(PosType pos) { maze[pos.row][pos.col]=0; } PosType NextPos(PosType curpos,int dir)/*//取当前通道块的下一个通道块*/ { switch(dir) {case 1: curpos.row++; break; case 2: curpos.col++; break; case 3: curpos.row--; break; case 4: curpos.col--; } return curpos;/*//将下一个通道块变为当前通道块*/ } int END(PosType curpos,PosType end) {return(curpos.row==end.row && curpos.col==end.col); } void MazePath(SqStack &s,PosType start,PosType end) {PosType curpos,nextpos; int curstep; SElemType e; SqStack *s; s=InitStack(); curpos=start; curstep=1; do{ if(Pass(curpos)) {FootPrint(curpos); e.ord=curstep;e.seat=curpos;e.di=1; Push(s,e); if(END(curpos,end)) return s; curpos=NextPos(curpos,1); curstep++; }/* end of if */ else { if(!StackEmpty(s)) { e=Pop(s); while(e.di==4 && !StackEmpty(s)) {FootPrint(e.seat);/* The same fuction as MarkPrint ? */ e=Pop(s); }/* end of while */ if(e.di<4) {e.di++;Push(s,e); curpos=NextPos(e.seat,e.di); } /* end of if */ } /* end of if */ } /* end of else */ }while(!StackEmpty(s)); curstep=0; return NULL; } void main() {SqStack *s; static PosType start={1,1},end={8,8}; s=MazePath(start,end); if(s) printpath(s); else printf("\n NO find the path!"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值