Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74178
  • 博文数量: 46
  • 博客积分: 560
  • 博客等级: 下士
  • 技术积分: 386
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-22 22:14
文章分类

全部博文(46)

文章存档

2013年(4)

2012年(42)

我的朋友

分类: C/C++

2012-06-21 14:43:48


点击(此处)折叠或打开

  1. /*
  2.  *题二:求解迷宫的一条最短路径,要求:用栈和队列实现,
  3.  *不允许使用递归算法。问题描述见教材第50页“3.2.4 迷宫求解”。
  4.  */

  5. #include <stdio.h>
  6. #include <malloc.h>
  7. #include <conio.h>

  8. #define ERROR 0
  9. #define OK 1
  10. #define OVERFLOW 0
  11. #define STACK_INIT_SIZE 100
  12. #define STACKINCREMENT 100

  13. typedef struct {
  14.     int x;
  15.     int y;
  16. }PosType;

  17. typedef struct {
  18.     int ord;
  19.     PosType seat;
  20.     int di;
  21. }ElemType;

  22. typedef struct {
  23.     ElemType *base;
  24.     ElemType *top;
  25.     int stacksize;
  26. }SqStack;

  27. int InitStack (SqStack *S)
  28. {
  29.     S->base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  30.     if (!S->base)
  31.          exit (OVERFLOW);
  32.     S->top=S->base;
  33.     S->stacksize=STACK_INIT_SIZE;
  34.     return OK;
  35. }

  36. int GetTop (SqStack S, ElemType *e)
  37. {
  38.     if (S.top==S.base)
  39.         return ERROR;
  40.     *e=*(S.top-1);
  41.     return OK;
  42. }

  43. int Push (SqStack *S, ElemType e)
  44. {
  45.     if (S->top - S->base >= S->stacksize)
  46.     {
  47.         S->base=(ElemType *)realloc(S->base,
  48.                               (S->stacksize + STACKINCREMENT)* sizeof(ElemType));
  49.         if (!S->base)
  50.             exit (OVERFLOW);
  51.         S->top=S->base+S->stacksize;
  52.         S->stacksize+=STACKINCREMENT;
  53.     }
  54.     *S->top++ = e;
  55.     return OK;
  56. }

  57. int Pop(SqStack *S,ElemType *e)
  58. {
  59.     if (S->top==S->base)
  60.         return ERROR;
  61.     *e=*(--S->top);
  62.     return OK;
  63. }

  64. int StackEmpty (SqStack S)
  65. {
  66.     if (S.base==S.top)
  67.         return 1;
  68.     else
  69.         return 0;
  70. }

  71. void ShowAll (SqStack *S)
  72. {
  73.     ElemType t;
  74.     printf ("h l step\n");
  75.     while (1)
  76.     {
  77.         if (Pop (S, &t))
  78.         {
  79.             printf ("%d %d %d\n", t.seat.x, t.seat.y, t.ord);
  80.         }
  81.         else
  82.             break;
  83.     }
  84. }

  85. void Change (SqStack *From, SqStack *To) /* 逆置矩阵 */
  86. {
  87.     ElemType t;
  88.     while (1)
  89.     {
  90.         if (Pop (From, &t))
  91.             Push (To, t);
  92.         else
  93.             break;
  94.     }
  95. }

  96. int Pass(int maze[10][10], PosType local) /*当前位置是否可走*/
  97. {

  98.     if (maze[local.x][local.y]==1)
  99.         return 0;
  100.     else
  101.         return 1;
  102. }

  103. void FootPrint (int maze[10][10],PosType local)/*修改当前位置为1*/
  104. {
  105.     maze[local.x][local.y]=1;
  106. }

  107. PosType NextPos (PosType local ,int i)
  108. {
  109.     if (i==1)
  110.     {
  111.         local.y+=1;
  112.     }
  113.     else if (i==2)
  114.     {
  115.         local.x+=1;
  116.     }
  117.     else if (i==3)
  118.     {
  119.         local.x-=1;
  120.     }
  121.     return local;
  122. }

  123. void MarkPrint (int maze[10][10],PosType local)/*修改当前位置为1*/
  124. {
  125.     maze[local.x][local.y]=1;
  126. }

  127. int MazePath (int maze[10][10],PosType start,PosType end,SqStack *S)
  128. {
  129.     PosType local = start;
  130.     ElemType temp;
  131.     int step=1;
  132.     do {
  133.         if (Pass (maze, local))
  134.         {
  135.             FootPrint (maze, local);
  136.             temp.ord=step;
  137.             temp.seat=local;
  138.             temp.di=1;
  139.             Push (S,temp);
  140.             if (Equal(local, end))
  141.                 return OK;
  142.             local = NextPos (local ,1);
  143.             step++;
  144.         }
  145.         else
  146.         {
  147.             if (!StackEmpty(*S))
  148.             {
  149.                 Pop(S,&temp);
  150.                 while (temp.di==4&&!StackEmpty(*S))
  151.                 {
  152.                     MarkPrint (maze,temp.seat);
  153.                     Pop (S,&temp);
  154.                     step--;
  155.                 }
  156.                 if (temp.di<4)
  157.                 {
  158.                     temp.di++;
  159.                     Push (S,temp);
  160.                     local=NextPos(temp.seat, temp.di);
  161.                }
  162.             }

  163.         }
  164.      }while (!StackEmpty (*S));
  165.      return ERROR;
  166. }

  167. int Equal (PosType A,PosType B)
  168. {
  169.     if (A.x==B.x&&A.y==B.y)
  170.         return 1;
  171.     else
  172.         return 0;
  173. }


  174. int main ()
  175. {
  176.     PosType start,end;
  177.     SqStack s;
  178.     int result=0;
  179.     int maze [10][10] =
  180.     { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  181.       {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
  182.       {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
  183.       {1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
  184.       {1, 0, 1, 1, 1, 0, 0, 0, 0, 1},
  185.       {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
  186.       {1, 0, 1, 0, 0, 0, 1, 0, 0, 1},
  187.       {1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
  188.       {1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  189.       {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
  190.     start.x=1;
  191.     start.y=1;
  192.     end.x=8;
  193.     end.y=8;
  194.     InitStack (&s);
  195.     result=MazePath (maze, start, end, &s);
  196.     if (result)
  197.     {
  198.         printf ("found\n");
  199.         ShowAll(&s);
  200.     }
  201.     else
  202.         printf ("not found");
  203.     getchar();
  204.     return 0;
  205. }

阅读(811) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~