迷宫(栈实现)

 

 

  1. #include<iostream>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. #define OK 1
  6. #define ERROR 0
  7. #define STACK_INIT_SIZE 100
  8. #define STACKINCREMENT 10
  9. #define OVERFLOW -2
  10. typedef int Status;
  11. typedef char ElemType; 
  12. typedef struct
  13. {
  14.         int x;
  15.         int y;
  16.         }PosType;
  17. typedef struct 
  18. {
  19.     int ord;    
  20.     PosType seat;
  21.     int di;    
  22.     }SElemType;
  23. typedef struct 
  24. {
  25.     SElemType *base,*top;
  26.     int stacksize;    
  27.     }SqStack;
  28. Status InitStack(SqStack &s)
  29. {
  30.    s.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
  31.    if(!s.base) exit(OVERFLOW);
  32.    s.top=s.base;
  33.    s.stacksize=STACK_INIT_SIZE;
  34.    return OK;} 
  35. Status Push(SqStack &s,SElemType e)
  36. {
  37.        if(s.top-s.base==s.stacksize){
  38.           s.base=(SElemType *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SElemType));
  39.           if(!s.base) exit(OVERFLOW);  
  40.           s.stacksize+=STACKINCREMENT;}
  41.        s.top->ord=e.ord;
  42.        s.top->seat=e.seat;
  43.        s.top->di=e.di;
  44.        ++s.top;
  45.        return OK;}
  46. Status Pop(SqStack &s,SElemType &e)
  47. {
  48.        if(s.base==s.top) return ERROR;
  49.        --s.top;
  50.        e.ord=s.top->ord;
  51.        e.seat=s.top->seat;
  52.        e.di=s.top->di;
  53.        return OK;}
  54. Status Pass(int (&maze)[10][10],PosType curpos) {
  55.   if (maze[curpos.x][curpos.y]==0)
  56.     return 1;     // 如果当前位置是可以通过,返回1
  57.   else return 0;  // 其它情况返回0
  58. }
  59. void FootPrint(int (&maze)[10][10],PosType curpos)    //标记走过的 
  60. {
  61.      maze[curpos.x][curpos.y]=-1;
  62.      }
  63. void MarkPrint(int (&maze)[10][10],PosType curpos) {    //取消标记 
  64.   maze[curpos.x][curpos.y]=0;
  65. }
  66. void NextPos(PosType &pos, int di)                    //下一个方向 
  67. {
  68.  switch(di){
  69.             case 1: (pos).y+=1; break;       //向东
  70.             case 2: (pos).x+=1; break;       //向南
  71.             case 3: (pos).y-=1; break;       //向西
  72.             case 4: (pos).x-=1; break;       //向北
  73.             }
  74. }
  75. Status StackEmpty(SqStack s)                     //判断栈空 
  76. {
  77.   if(s.top==s.base)
  78.       return true;
  79.   else
  80.       return false;     }
  81. Status MazePath(int (&maze)[10][10],PosType start,PosType end)    //走迷宫 
  82. {
  83.        SqStack S;
  84.        PosType curpos=start;
  85.        int curstep=1;
  86.        SElemType e;
  87.        InitStack(S);
  88.        do
  89.        {
  90.          if(Pass(maze,curpos)) {
  91.          FootPrint(maze,curpos);
  92.          e.ord = curstep;
  93.          e.seat= curpos;
  94.          e.di =1;
  95.          Push(S,e);
  96.          if(curpos.x==end.x&&curpos.y==end.y)
  97.             return true;  
  98.          NextPos(curpos, 1);
  99.          curstep++;}
  100.          else {
  101.               if (!StackEmpty(S)) {
  102.                   
  103.                   while(e.di==4&&!StackEmpty(S))
  104.                     {
  105.                     Pop(S,e);
  106.                     MarkPrint(maze,e.seat);}Pop(S,e);
  107.                   if(e.di<4) {
  108.                      e.di++;
  109.                      Push (S,e); 
  110.                      NextPos(e.seat, e.di);
  111.                      curpos=e.seat;}
  112.                      }
  113.               }
  114.          }while(!StackEmpty(S));
  115.          return false;
  116. }
  117. void Print(int maze[10][10])          //输出迷宫,路径用*标出,墙壁用#,可通过的地方用空格
  118. {
  119.      for(int i=0;i<10;i++)
  120.         {for(int j=0;j<10;j++)
  121.         if(maze[i][j]==0) cout<<" ";
  122.         else if(maze[i][j]==-1) cout<<"*";
  123.         else cout<<"#";
  124.         cout<<endl;}
  125.      cout<<endl;
  126. }
  127. int main()
  128. {
  129.    int m[10][10]={
  130.              {1,1,1,1,1,1,1,1,1,1},
  131.              {1,0,0,1,0,0,0,1,0,1},
  132.              {1,0,0,1,0,0,0,1,0,1},
  133.              {1,0,0,0,0,1,1,0,0,1},
  134.              {1,0,1,1,1,0,0,0,0,1},
  135.              {1,0,0,0,1,0,0,0,0,1},
  136.              {1,0,1,0,0,0,1,0,0,1},
  137.              {1,0,1,1,1,0,1,1,0,1},
  138.              {1,1,0,0,0,0,0,0,0,1},
  139.              {1,1,1,1,1,1,1,1,1,1}};
  140.  Print(m);         
  141.  PosType start,end;
  142.  start.x=start.y=1;
  143.  end.x=end.y=8;            
  144. if(MazePath(m,start,end))
  145. Print(m);
  146. return 0;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值