严蔚敏 数据结构 课本中 栈应用 走迷宫 C语言 完整版

来源:互联网 发布:淘宝最迟几天确认收货 编辑:程序博客网 时间:2024/05/06 07:57
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. #define STACK_INIT_SIZE 100  
  6. #define INCREMENT 10  
  7.   
  8. /*  typedef int ElemType;  */  
  9.   
  10.   
  11. typedef struct{  
  12.     int r;  
  13.     int c;  
  14. }PosType;  
  15.   
  16. typedef struct{  
  17.     int ord;          /* 通道块在路径上的序号  */  
  18.     PosType seat;    /*  通道块在迷宫中的“坐标位置”  */  
  19.     int di;           /*  从此通道块走向下一个通道块的“方向” */  
  20. }ElemType;  
  21.   
  22. typedef struct{  
  23.     int arr[10][10];  
  24. }MazeType;  
  25.   
  26.   
  27. typedef struct SqStack  
  28. {  
  29.     ElemType *base;  
  30.     ElemType *top;  
  31.     int size;  
  32. }SqStack;  
  33.   
  34. int initStack(SqStack *s)  
  35. {  
  36.     s->base = (ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType) );  
  37.     if(!s->base)  
  38.         return -1;  
  39.     s->top = s->base;  
  40.     s->size = STACK_INIT_SIZE;  
  41.     return 0;  
  42. }  
  43.   
  44. int getTop(SqStack s,ElemType *e)  
  45. {  
  46.     if(s.base!=s.top)  
  47.         *e = *(s.top-1);  
  48.     return -1;  
  49. }  
  50.   
  51. int push(SqStack *s, ElemType e)  
  52. {  
  53.     if(s->top - s->base >= s->size)  
  54.     {  
  55.          s->base = (ElemType *)realloc(s->base, (s->size+INCREMENT)*sizeof(ElemType));  
  56.          if(!s->base)  
  57.             return -1;  
  58.          s->top = s->base+s->size;  
  59.          s->size += INCREMENT;  
  60.     }  
  61.   
  62.     *s->top++ = e;  
  63.     return 0;  
  64. }  
  65.   
  66. int pop(SqStack *s,ElemType *e)  
  67. {  
  68.     if(s->top == s->base)  
  69.         return -1;  
  70.     *e = *(--s->top);  
  71.     return 0;  
  72. }  
  73.   
  74. int isEmpty(SqStack *s)  
  75. {  
  76.     if(s->base == s->top)  
  77.         return 1;  
  78.     else  
  79.         return 0;  
  80. }  
  81.   
  82. int clearStack(SqStack *s)  
  83. {  
  84.     ElemType e;  
  85.     if(!s) return -1;  
  86.     if(s->base == s->top) return 0;  
  87.     while(!isEmpty(s))  
  88.     {  
  89.         pop(s,&e);  
  90.     }  
  91.     return 0;  
  92. }  
  93.   
  94. int pass(MazeType MyMaze, PosType CurPos);  
  95. void footPrint(MazeType &MyMaze, PosType CurPos);  
  96. void markPrint(MazeType &MyMaze, PosType CurPos);  
  97. PosType nextPos(PosType CurPos, int Dir);  
  98.   
  99.   
  100. int pass( MazeType MyMaze,PosType CurPos) {  
  101.   if (MyMaze.arr[CurPos.r][CurPos.c]==' ')  
  102.     return 1;     // 如果当前位置是可以通过,返回1  
  103.   else return 0;  // 其它情况返回0  
  104. }  
  105. void footPrint(MazeType &MyMaze,PosType CurPos) {  
  106.   MyMaze.arr[CurPos.r][CurPos.c]='*';  
  107. }  
  108. void markPrint(MazeType &MyMaze,PosType CurPos) {  
  109.   MyMaze.arr[CurPos.r][CurPos.c]='!';  
  110. }  
  111.   
  112. PosType nextPos(PosType CurPos, int Dir) {  
  113.   PosType ReturnPos;  
  114.   switch (Dir) {  
  115.     case 1:  
  116.         ReturnPos.r=CurPos.r;  
  117.         ReturnPos.c=CurPos.c+1;  
  118.         break;  
  119.     case 2:  
  120.         ReturnPos.r=CurPos.r+1;  
  121.         ReturnPos.c=CurPos.c;  
  122.         break;  
  123.     case 3:  
  124.         ReturnPos.r=CurPos.r;  
  125.         ReturnPos.c=CurPos.c-1;  
  126.         break;  
  127.     case 4:  
  128.         ReturnPos.r=CurPos.r-1;  
  129.         ReturnPos.c=CurPos.c;  
  130.         break;  
  131.   }  
  132.   return ReturnPos;  
  133. }  
  134.   
  135.   
  136. /* 迷宫函数  */  
  137. /* 判断是否存在一条从开口到结尾的路径 */  
  138. int mazePath(MazeType &maze, PosType start, PosType end)  
  139. {  
  140.     SqStack *s = (SqStack *)malloc(sizeof(SqStack));  
  141.     initStack(s);  
  142.     PosType curpos = start;  // 设定"当前位置"为"入口位置"  
  143.     ElemType e;  
  144.     int curstep = 1;     // 探索第一步  
  145.   do {  
  146.     if (pass(maze,curpos)) {  // 当前位置可通过,即是未曾走到过的通道块  
  147.       footPrint(maze,curpos); // 留下足迹  
  148.       e.di =1;  
  149.       e.ord = curstep;  
  150.       e.seat= curpos;  
  151.       push(s,e);              // 加入路径  
  152.       if (curpos.r == end.r && curpos.c==end.c)  
  153.         return 0;        // 到达终点(出口)  
  154.       curpos = nextPos(curpos, 1);        // 下一位置是当前位置的东邻  
  155.       curstep++;                          // 探索下一步  
  156.     } else {  // 当前位置不能通过  
  157.       if (!isEmpty(s)) {  
  158.         pop(s,&e);  
  159.         while (e.di==4 && !isEmpty(s)) {  
  160.           markPrint(maze,e.seat);  
  161.           pop(s,&e);    // 留下不能通过的标记,并退回一步  
  162.         } // while  
  163.         if (e.di<4) {  
  164.           e.di++;  
  165.           push(s, e);  // 换下一个方向探索  
  166.           curpos = nextPos(e.seat, e.di); // 当前位置设为新方向的相邻块  
  167.         } // if  
  168.       } // if  
  169.     } // else  
  170.   } while (!isEmpty(s) );  
  171.   return -1;  
  172. // MazePath  
  173.   
  174.   
  175.   
  176. int main()  
  177. {  
  178.     //printf("Hello world!");  
  179.     return 0;  
  180. }  
  181. 原文链接:http://blog.csdn.net/daringpig/article/details/7657407
0 0
原创粉丝点击