Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1867567
  • 博文数量: 152
  • 博客积分: 3730
  • 博客等级: 上尉
  • 技术积分: 3710
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:36
个人简介

减肥,运动,学习,进步...

文章分类

全部博文(152)

文章存档

2016年(14)

2015年(17)

2014年(16)

2013年(4)

2012年(66)

2011年(35)

分类: LINUX

2012-04-06 10:33:47

堆栈是计算机程序中非常重要的一部分,主要用来参数的调用,局部变量的存储等,在C语言中的函数调用过程中通过不同函数的堆栈空间可以非常方便的找到传递进来的参数以及退出时应该返回的地址。具体的参看“ ”,这篇文章中通过实例分析讨论了函数调用过程中堆栈的变化过程。
 
实质上栈的运用并不是只能在函数调用中,栈作为一种数据结构,自然有其特殊的意义,栈的最大特点是"先入后出,后入先出",这个特点可以用来结局很多的问题。C语言中的函数调用算是其中的最主要的用法之一,也就不再分析和讨论。同样递归,嵌套调用等都属于函数调用的子类也不再描述。在其他方面经典的运用是解决迷宫问题,不同进制数值之间的转换,长字符串的分解以及算术表达式的求值等。下面我主要采用栈实现经典的迷宫问题。
迷宫问题
迷宫问题是经典的一类问题,如何从给出的入口找到对应的出口,实现的方法和马踏棋盘问题相似也是通过找到周围8个方向坐标的关系,然后依据深度优先搜索方法和一定的条件找到下一步对应的出路。由于迷宫问题需要存储具体的完成路劲,这与前面的问题存在一定的差别。采用栈能够很好的解决这个问题,其中栈结构用来存储具体的坐标和方向。这样根据栈就能保证以后每一次都能快速的找到出路。
实现的基本步骤如下:
1、为了避免边界检测问题,在迷宫的外围添加一层围墙,比如原来的迷宫为m*n,则添加围墙以后的矩阵为(m+2)*(n+2)。其中为1表示不能走通,0时表示可以走通。这样maze[1][1]表示迷宫的入口,而maze[m][n]表示迷宫的出口。
2、创建一个堆栈空间,可以采用静态数组的方式,但由于不能准确的估计数组空间大小,可以采用动态创建的方式。并将入口坐标值压入到栈中。定义一个与迷宫大小相同的矩阵mark,用来统计当前坐标是否已经到达过,当没有到达坐标(i,j)时,则有mark[i][j] = 0,如果之前到达过,则有mark[i][j] = 1.这样主要是为了避免形成内部死循环,同时说明此路不能走通。
3、检测栈空间是否为空,如果为空则停止,如果不为空,则弹出栈顶的元素.
4、采用循环的方式,依据元素的方向确定下一个坐标,具体的确定方法依据之前的移动关系找到,判断该位置是否为0(maze[nextrow][nextcol] == 0)以及之前是否到达该位置(mark[nextrow][nextcol] == 1)。如果满足条件,则将mark[nextrow][newcol]设置为1,并将当前位置以及具体的方向值压入栈中,将当前坐标设置为之前确定的下一个坐标,设置方向为0。然后重新进行步骤4。如果8个方向全部不能找到合适的下一个坐标,说明此路走不通。重新进行步骤3,探索新的路劲。探索成功的条件是(nextrow == EXIT_ROW&&nextcol == EXIT_COL)。
基本的实现流程图如下所示:

代码实现如下:

点击(此处)折叠或打开

  1. /*maze_problem.h*/
  2. #ifndef MAZE_PROBLEM_H_H_
  3. #define MAZE_PROBLEM_H_H_

  4. typedef struct
  5. {
  6.     int vert;
  7.     int horiz;
  8. }offsets;

  9. typedef struct {
  10.     int row;
  11.     int col;
  12.     int dir;
  13. }element;

  14. typedef struct {
  15.     int row;
  16.     int col;
  17. }coordinate;
  18. #endif

点击(此处)折叠或打开

  1. /*maze_problem.c*/
  2. #include "maze_problem.h"

  3. #include<stdio.h>
  4. #include<stdlib.h>

  5. offsets move[8];

  6. /*the stack save the path, and used */
  7. element * stack;
  8. int top = -1;

  9. void initial_move(void)
  10. {
  11.     /*horiz means cols*/
  12.     move[0].horiz = 0;
  13.     /*vert means rows*/
  14.     move[0].vert = -1;
  15.     
  16.     move[1].horiz = 1;
  17.     move[1].vert = -1;
  18.     
  19.     move[2].horiz = 1;
  20.     move[2].vert = 0;
  21.     
  22.     move[3].horiz = 1;
  23.     move[3].vert = 1;

  24.     move[4].horiz = 0;
  25.     move[4].vert = 1;
  26.     
  27.     move[5].horiz = -1;
  28.     move[5].vert = 1;
  29.     
  30.     move[6].horiz = -1;
  31.     move[6].vert = 0;
  32.     
  33.     move[7].horiz = -1;
  34.     move[7].vert = -1;
  35. }
  36. #define MAZE_ROWS    12
  37. #define MAZE_COLS    15

  38. #define NEW_MAZE_ROWS (MAZE_ROWS + 2)
  39. #define NEW_MAZE_COLS (MAZE_COLS + 2)
  40. #define EXIT_COL    15
  41. #define EXIT_ROW    12

  42. int maze[NEW_MAZE_ROWS][NEW_MAZE_COLS]
  43. = {
  44.  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  45.  1,0,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1,
  46.  1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1,
  47.  1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1,
  48.  1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,0,1,
  49.  1,1,1,0,1,0,0,1,0,1,1,1,1,1,1,1,1,
  50.  1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1,
  51.  1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1,
  52.  1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,
  53.  1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,
  54.  1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1,
  55.  1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1,
  56.  1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,0,1,
  57.  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  58. };

  59. /*used to store the used place*/
  60. int mark[NEW_MAZE_ROWS][NEW_MAZE_COLS];

  61. void mark_init()
  62. {
  63.     int i = 0,j = 0;
  64.     for(i = 0; i < NEW_MAZE_ROWS ; ++ i)
  65.         for(j = 0; j < NEW_MAZE_COLS ; ++ j)
  66.             mark[i][j] = 0;
  67. }
  68. int mark_stack_size(int maze[NEW_MAZE_ROWS][NEW_MAZE_COLS])
  69. {
  70.     int i = 0,j = 0;

  71.     int size = 0;
  72.     for(i = 0; i < NEW_MAZE_ROWS; ++ i)
  73.         for (j = 0; j < NEW_MAZE_COLS ; ++ j)
  74.         {
  75.             if(!maze[i][j])
  76.             size ++;
  77.         }    
  78.     return size;
  79. }

  80. coordinate nextposition(element a,int dir)
  81. {
  82.     coordinate b;
  83.     b.col = a.col + move[dir].horiz;
  84.     b.row = a.row + move[dir].vert;
  85.     
  86.     return b;
  87. }

  88. int maze_out()
  89. {
  90.     element temp;
  91.     coordinate nextp;
  92.     
  93.     /*Test the stack is not empty*/
  94.     while(top >= 0)
  95.     {
  96.         /*pop a element*/
  97.         temp = *(stack+top);
  98.         top --;

  99.         /*find on eight directions*/
  100.         while(temp.dir < 8)
  101.         {
  102.             /*get the possible next positions*/
  103.             nextp = nextposition(temp,temp.dir);
  104.             /*next direction*/
  105.             temp.dir ++;

  106.             /*success conditions*/
  107.             if(nextp.row == EXIT_ROW &&
  108.              nextp.col == EXIT_COL)
  109.             {
  110.                 /*save current position*/
  111.                 stack[++top] = temp;

  112.                 /*save the exit pointion*/
  113.                 stack[++top].row = EXIT_ROW;
  114.                 stack[top].col = EXIT_COL;
  115.                 stack[top].dir = 0;

  116.                 /*exit*/
  117.                 return 1;
  118.             }

  119.             /*the new position is ok and does not wake*/
  120.             if(maze[nextp.row][nextp.col] ==0 &&
  121.              mark[nextp.row][nextp.col] == 0)
  122.             {
  123.                 /*mark means that this way has been waked*/
  124.                 mark[nextp.row][nextp.col] = 1;

  125.                 /*
  126.                  *push a element in stack
  127.                  *save current position and direction
  128.                  *if this way is failed, used to this position to start new way.
  129.                 */
  130.                 stack[++top] = temp;
  131.                 
  132.                 /*go to the new position as current position*/
  133.                 temp.row = nextp.row;
  134.                 temp.col = nextp.col;
  135.                 temp.dir = 0;
  136.             }
  137.         }
  138.     }    
  139.     /*failed*/
  140.     return 0;
  141. }

  142. int main()
  143. {
  144.     int i = 0;
  145.     /*inital the mark array*/
  146.     mark_init();
  147.     initial_move();

  148.     /*create stack*/
  149.     stack = (element*)malloc(mark_stack_size(maze)*sizeof(element));
  150.     /*if failed*/
  151.     if(stack == NULL)
  152.         return 0;
  153.     /*push a element in stack*/
  154.     top ++;
  155.     (stack+top)->col = 1;
  156.     (stack+top)->row = 1;
  157.     (stack+top)->dir = 0;

  158.     if(maze_out())
  159.     {
  160.         while(i <= top)
  161.         {
  162.             printf("(%d,%d,%d)\n",stack[i].row,stack[i].col,stack[i].dir);
  163.             i ++;
  164.         }
  165.     //    printf("(%d,%d)\n",EXIT_ROW,EXIT_COL);

  166.     }
  167.     /*free the memory*/
  168.     free(stack);
  169.     /*point to the NULL*/
  170.     stack = NULL;
  171.     return 1;
  172. }
测试结果:

在迷宫问题中,栈主要实现了对满足条件坐标以及方向值(0-7,分别表示一个具体的方向)的动态保存能够保证路劲的一致性,也就是先入后出的特性。

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