堆棧是計算機程序中非常重要的一部分,主要用來參數的調用,局部變量的存儲等,在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)。
代碼實現如下:
/*maze_problem.h*/
#ifndef MAZE_PROBLEM_H_H_
#define MAZE_PROBLEM_H_H_
typedef struct
{
int vert;
int horiz;
}offsets;
typedef struct {
int row;
int col;
int dir;
}element;
typedef struct {
int row;
int col;
}coordinate;
#endif
/*maze_problem.c*/
#include "maze_problem.h"
#include<stdio.h>
#include<stdlib.h>
offsets move[8];
/*the stack save the path, and used */
element * stack;
int top = -1;
void initial_move(void)
{
/*horiz means cols*/
move[0].horiz = 0;
/*vert means rows*/
move[0].vert = -1;
move[1].horiz = 1;
move[1].vert = -1;
move[2].horiz = 1;
move[2].vert = 0;
move[3].horiz = 1;
move[3].vert = 1;
move[4].horiz = 0;
move[4].vert = 1;
move[5].horiz = -1;
move[5].vert = 1;
move[6].horiz = -1;
move[6].vert = 0;
move[7].horiz = -1;
move[7].vert = -1;
}
#define MAZE_ROWS 12
#define MAZE_COLS 15
#define NEW_MAZE_ROWS (MAZE_ROWS + 2)
#define NEW_MAZE_COLS (MAZE_COLS + 2)
#define EXIT_COL 15
#define EXIT_ROW 12
int maze[NEW_MAZE_ROWS][NEW_MAZE_COLS]
= {
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1,
1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1,
1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1,
1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,0,1,
1,1,1,0,1,0,0,1,0,1,1,1,1,1,1,1,1,
1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1,
1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1,
1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,
1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,
1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1,
1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1,
1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
/*used to store the used place*/
int mark[NEW_MAZE_ROWS][NEW_MAZE_COLS];
void mark_init()
{
int i = 0,j = 0;
for(i = 0; i < NEW_MAZE_ROWS ; ++ i)
for(j = 0; j < NEW_MAZE_COLS ; ++ j)
mark[i][j] = 0;
}
int mark_stack_size(int maze[NEW_MAZE_ROWS][NEW_MAZE_COLS])
{
int i = 0,j = 0;
int size = 0;
for(i = 0; i < NEW_MAZE_ROWS; ++ i)
for (j = 0; j < NEW_MAZE_COLS ; ++ j)
{
if(!maze[i][j])
size ++;
}
return size;
}
coordinate nextposition(element a,int dir)
{
coordinate b;
b.col = a.col + move[dir].horiz;
b.row = a.row + move[dir].vert;
return b;
}
int maze_out()
{
element temp;
coordinate nextp;
/*Test the stack is not empty*/
while(top >= 0)
{
/*pop a element*/
temp = *(stack+top);
top --;
/*find on eight directions*/
while(temp.dir < 8)
{
/*get the possible next positions*/
nextp = nextposition(temp,temp.dir);
/*next direction*/
temp.dir ++;
/*success conditions*/
if(nextp.row == EXIT_ROW &&
nextp.col == EXIT_COL)
{
/*save current position*/
stack[++top] = temp;
/*save the exit pointion*/
stack[++top].row = EXIT_ROW;
stack[top].col = EXIT_COL;
stack[top].dir = 0;
/*exit*/
return 1;
}
/*the new position is ok and does not wake*/
if(maze[nextp.row][nextp.col] ==0 &&
mark[nextp.row][nextp.col] == 0)
{
/*mark means that this way has been waked*/
mark[nextp.row][nextp.col] = 1;
/*
*push a element in stack
*save current position and direction
*if this way is failed, used to this position to start new way.
*/
stack[++top] = temp;
/*go to the new position as current position*/
temp.row = nextp.row;
temp.col = nextp.col;
temp.dir = 0;
}
}
}
/*failed*/
return 0;
}
int main()
{
int i = 0;
/*inital the mark array*/
mark_init();
initial_move();
/*create stack*/
stack = (element*)malloc(mark_stack_size(maze)*sizeof(element));
/*if failed*/
if(stack == NULL)
return 0;
/*push a element in stack*/
top ++;
(stack+top)->col = 1;
(stack+top)->row = 1;
(stack+top)->dir = 0;
if(maze_out())
{
while(i <= top)
{
printf("(%d,%d,%d)\n",stack[i].row,stack[i].col,stack[i].dir);
i ++;
}
// printf("(%d,%d)\n",EXIT_ROW,EXIT_COL);
}
/*free the memory*/
free(stack);
/*point to the NULL*/
stack = NULL;
return 1;
}
在迷宮問題中,棧主要實現了對滿足條件坐標以及方向值(0-7,分別表示一個具體的方向)的動態保存能夠保證路勁的一致性,也就是先入后出的特性。