棧 深度優先搜尋與回溯演算法求解迷宮

2021-05-25 18:52:19 字數 1672 閱讀 6568

利用棧和回溯演算法求解迷宮:

這是老師帶著做的,自己寫不知道什麼時候能調出來~~

這就是和老師的差距,這樣的程式他就10幾分鐘而已~

code:

/*深度优先�索求解迷宫*/

#include

#define max_row 5

#define max_col 5

int maze[max_row][max_col] = ,  

,  ,  

,  ,  

};  

struct pointstack[512];  

struct point front[max_row][max_col]=, , , , },  

, , , , },  

, , , , },  

, , , , },  

, , , , },  

};  

int top = 0;  

struct point start = ;  

struct point target = ;  

struct point full;  

void push(struct point p)  

struct point pop(void)  

int is_empty(void)  

struct point get_top(void)  

void print_stack(void)  

}  void print_maze(void)  

printf("*****************/n");  

}  void print_pre()  

printf("/n");  

}  }  

void print_front(void)  

printf("(%d,%d)/n", start.row, start.col);  

}  void visit(int newrow, int newcol, struct point p)  

void back_track(struct point p)  

}  /*void back_track_full(struct point p)

*/int main(viod)  

flag = 0;  

/*expend p to up. left,down, right*/

if( ((p.row - 1) >= 0) && (maze[p.row - 1][p.col] == 0) )/*up*/

if( ((p.col - 1) >= 0) && (maze[p.row][p.col - 1] == 0) )/*left*/

if( ((p.row + 1) < max_row) && (maze[p.row + 1][p.col] == 0) )/*down*/

if( ((p.col + 1) < max_col) && (maze[p.row][p.col + 1] == 0) )/*right*/

if(flag == 0)  

back_track(p);    

print_stack();  

print_pre();  

//      getchar();

}  return 0;  

}  

深度優先搜尋演算法求解TSP問題

問題描述 採用深度優先搜尋演算法求解tsp問題,並在搜尋過程中,使用界限條件 當前結點已經走過的路徑長度要小於已求得的最短路徑 進行 剪枝 操作 不再對後續結點進行遍歷 從而提高搜尋效率。採用queue模組中的棧 lifoqueue 來實現深度優先搜尋。輸入形式 在螢幕上輸入頂點個數和連線頂點間的邊...

回溯法遵循深度優先嗎 深度優先搜尋(回溯法)

事實上,深度優先搜尋屬於圖演算法的一種,英文縮寫為dfs即depth first search.其過程簡要來說是對每乙個可能的分支路徑深入到不能再深入為止,而且每個節點只能訪問一次.舉例說明之 下圖是乙個無向圖,如果我們從a點發起深度優先搜尋 以下的訪問次序並不是唯一的,第二個點既可以是b也可以是c...

深度優先與廣度優先搜尋演算法

1.深度優先搜尋演算法 depth first search.其過程簡要來說是對每乙個可能的分支路徑深入到不能再深入為止,而且每個節點只能訪問一次.深度優先遍歷圖的方法是,從圖中某頂點v出發 1 訪問頂點v 2 依次從v的未被訪問的鄰接點出發,對圖進行深度優先遍歷 直至圖中和v有路徑相通的頂點都被訪...