小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

棧的妙用-實(shí)現(xiàn)迷宮問題(轉(zhuǎn))

 WUCANADA 2013-04-16

分類: LINUX

堆棧是計(jì)算機(jī)程序中非常重要的一部分,主要用來參數(shù)的調(diào)用,局部變量的存儲等,在C語言中的函數(shù)調(diào)用過程中通過不同函數(shù)的堆??臻g可以非常方便的找到傳遞進(jìn)來的參數(shù)以及退出時應(yīng)該返回的地址。具體的參看“函數(shù)調(diào)用分析 ”,這篇文章中通過實(shí)例分析討論了函數(shù)調(diào)用過程中堆棧的變化過程。
 
實(shí)質(zhì)上棧的運(yùn)用并不是只能在函數(shù)調(diào)用中,棧作為一種數(shù)據(jù)結(jié)構(gòu),自然有其特殊的意義,棧的最大特點(diǎn)是"先入后出,后入先出",這個特點(diǎn)可以用來結(jié) 局很多的問題。C語言中的函數(shù)調(diào)用算是其中的最主要的用法之一,也就不再分析和討論。同樣遞歸,嵌套調(diào)用等都屬于函數(shù)調(diào)用的子類也不再描述。在其他方面經(jīng) 典的運(yùn)用是解決迷宮問題,不同進(jìn)制數(shù)值之間的轉(zhuǎn)換,長字符串的分解以及算術(shù)表達(dá)式的求值等。下面我主要采用棧實(shí)現(xiàn)經(jīng)典的迷宮問題。
迷宮問題
迷宮問題是經(jīng)典的一類問題,如何從給出的入口找到對應(yīng)的出口,實(shí)現(xiàn)的方法和馬踏棋盤問題相似也是通過找到周圍8個方向坐標(biāo)的關(guān)系,然后依據(jù)深度 優(yōu)先搜索方法和一定的條件找到下一步對應(yīng)的出路。由于迷宮問題需要存儲具體的完成路勁,這與前面的問題存在一定的差別。采用棧能夠很好的解決這個問題,其 中棧結(jié)構(gòu)用來存儲具體的坐標(biāo)和方向。這樣根據(jù)棧就能保證以后每一次都能快速的找到出路。
實(shí)現(xiàn)的基本步驟如下:
1、為了避免邊界檢測問題,在迷宮的外圍添加一層圍墻,比如原來的迷宮為m*n,則添加圍墻以后的矩陣為(m+2)*(n+2)。其中為1表示不能走通,0時表示可以走通。這樣maze[1][1]表示迷宮的入口,而maze[m][n]表示迷宮的出口。
2、創(chuàng)建一個堆??臻g,可以采用靜態(tài)數(shù)組的方式,但由于不能準(zhǔn)確的估計(jì)數(shù)組空間大小,可以采用動態(tài)創(chuàng)建的方式。并將入口坐標(biāo)值壓入到棧中。定義 一個與迷宮大小相同的矩陣mark,用來統(tǒng)計(jì)當(dāng)前坐標(biāo)是否已經(jīng)到達(dá)過,當(dāng)沒有到達(dá)坐標(biāo)(i,j)時,則有mark[i][j] = 0,如果之前到達(dá)過,則有mark[i][j] = 1.這樣主要是為了避免形成內(nèi)部死循環(huán),同時說明此路不能走通。
3、檢測??臻g是否為空,如果為空則停止,如果不為空,則彈出棧頂?shù)脑?
4、采用循環(huán)的方式,依據(jù)元素的方向確定下一個坐標(biāo),具體的確定方法依據(jù)之前的移動關(guān)系找到,判斷該位置是否為0(maze[nextrow] [nextcol] == 0)以及之前是否到達(dá)該位置(mark[nextrow][nextcol] == 1)。如果滿足條件,則將mark[nextrow][newcol]設(shè)置為1,并將當(dāng)前位置以及具體的方向值壓入棧中,將當(dāng)前坐標(biāo)設(shè)置為之前確定的下一 個坐標(biāo),設(shè)置方向?yàn)?。然后重新進(jìn)行步驟4。如果8個方向全部不能找到合適的下一個坐標(biāo),說明此路走不通。重新進(jìn)行步驟3,探索新的路勁。探索成功的條件 是(nextrow == EXIT_ROW&&nextcol == EXIT_COL)。
基本的實(shí)現(xiàn)流程圖如下所示:

代碼實(shí)現(xiàn)如下:

點(diǎn)擊(此處)折疊或打開

  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

點(diǎn)擊(此處)折疊或打開

  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. }
測試結(jié)果:

在迷宮問題中,棧主要實(shí)現(xiàn)了對滿足條件坐標(biāo)以及方向值(0-7,分別表示一個具體的方向)的動態(tài)保存能夠保證路勁的一致性,也就是先入后出的特性。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多