日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用栈求解迷宫问题

發布時間:2025/4/5 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用栈求解迷宫问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 使用棧求解迷宮問題

1 使用棧求解迷宮問題

找迷宮通路需要使用回溯法,找迷宮通路是對回溯法的一個很好的應用,實現回溯的過程用到數據結構—棧!

回溯法: 對一個包括有很多個結點,每個結點有若干個搜索分支的問題,把原問題分解為若干個子問題求解的算法;當搜索到某個結點發現無法再繼續搜索下去時,就讓搜索過程回溯(回退)到該節點的前一個結點,繼續搜索該節點外的其他尚未搜索的分支;如果發現該結點無法再搜索下去,就讓搜索過程回溯到這個結點的前一結點繼續這樣的搜索過程;這樣的搜索過程一直進行到搜索到問題的解或者搜索完了全部可搜索分支沒有解存在為止。

地圖如下:

完整代碼如下:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h>#define ROW 6 #define COL 6typedef struct _Maze {int map[ROW][COL]; }Maze;#define MAXSIZE 100typedef struct _Position {//迷宮坐標int _x;int _y; }Position;#define MaxSize 128 //預先分配空間,這個數值根據實際需要預估確定typedef Position ElemType;typedef struct _SqStack {ElemType* base; //棧底指針ElemType* top; //棧頂指針 }SqStack;bool InitStack(SqStack& S) //構造一個空棧 S {S.base = new ElemType[MaxSize];//為順序棧分配一個最大容量為 Maxsize的空間if (!S.base) //空間分配失敗return false;S.top = S.base; //top 初始為 base,空棧return true; }bool PushStack(SqStack& S, ElemType e) // 插入元素 e 為新的棧頂元素 {if (S.top - S.base == MaxSize) //棧滿return false;*(S.top++) = e; //元素 e 壓入棧頂,然后棧頂指針加 1,等價于*S.top=e;S.top++;return true; } bool PopStack(SqStack & S, ElemType & e) //刪除 S 的棧頂元素,暫存在變量 e中 {if (S.base == S.top) { //???/span>return false;}e = *(--S.top); //棧頂指針減 1,將棧頂元素賦給 ereturn true; }ElemType* GetTop(SqStack & S) //返回 S 的棧頂元素,棧頂指針不變 {if (S.top != S.base) { //棧非空return S.top - 1; //返回棧頂元素的值,棧頂指針不變}else {return NULL;} }int GetSize(SqStack & S) {//返回棧中元素個數return (S.top - S.base); }bool IsEmpty(SqStack & S) {//判斷棧是否為空if (S.top == S.base) {return true;}else {return false;} }void DestoryStack(SqStack & S) {//銷毀棧if (S.base) {free(S.base);S.base = NULL;S.top = NULL;} }void InitMaze(Maze* m, int map[ROW][COL]) //迷宮的初始化 {for (int i = 0; i < ROW; ++i){for (int j = 0; j < COL; ++j){m->map[i][j] = map[i][j];}} }void PrintMaze(Maze* m) //打印迷宮 {for (int i = 0; i < ROW; ++i){for (int j = 0; j < COL; ++j){printf("%d ", m->map[i][j]);}printf("\n");}printf("\n"); }int IsValidEnter(Maze* m, Position cur) //判斷是否是有效的入口 {assert(m);if ((cur._x == 0 || cur._x == ROW - 1)|| (cur._y == 0 || cur._y == COL - 1)&& (m->map[cur._x][cur._y] == 1))return 1;elsereturn 0; }int IsNextPass(Maze* m, Position cur, Position next) //判斷當前節點的下一個節點能否走通 {assert(m);//判斷 next 節點是否為 cur 的下一節點if (((next._x == cur._x) && ((next._y == cur._y + 1) || (next._y ==cur._y - 1))) //在同一行上并且相鄰|| ((next._y == cur._y) && ((next._x == cur._x + 1) || (next._x ==cur._x - 1)))) {//或在同一列上并且相鄰//判斷下一個節點是否在迷宮里面if (((next._x >= 0 && next._x < ROW) || (next._y >= 0 && next._y< COL))&& (m->map[next._x][next._y] == 1)) {return 1;}}return 0; }int IsValidExit(Maze* m, Position cur, Position enter) //判斷當前節點是不是有效的迷宮出口 {assert(m);//這里首先得保證該節點不是入口點,其次只要它處在迷宮的邊界即可if ((cur._x != enter._x || cur._y != enter._y)&& ((cur._x == 0 || cur._x == ROW - 1)|| (cur._y == 0 || cur._y == COL - 1))){return 1;}elsereturn 0; }int PassMaze(Maze* m, Position enter, SqStack* s) //找迷宮通路 {assert(m && IsValidEnter(m, enter) == 1); //對給的迷宮的入口進行合法性判斷Position cur = enter;Position next;PushStack(*s, cur); //首先將迷宮的入口壓入棧中m->map[cur._x][cur._y] = 2; //將入口值改為 2while (!IsEmpty(*s)) {cur = *GetTop(*s);//printf("cur: %d %d\n",cur._x, cur._y);if (IsValidExit(m, cur, enter) == 1) //判斷當前位置是否出口return 1;//嘗試向左一步:看當前節點的左一個節點能不能走通next = cur;next._y = cur._y - 1;if (IsNextPass(m, cur, next) == 1){PushStack(*s, next);m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;//PrintMaze(m);continue;}//嘗試向上一步:看當前節點的上一個節點能不能走通next = cur;next._x = cur._x - 1;if (IsNextPass(m, cur, next) == 1) //next 節點能夠走通時,將其壓入棧中{PushStack(*s,next);m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1; //將next 節點的值等于 cur 節點的值加 1//PrintMaze(m);continue;}//右:看當前節點的向右的一個節點能不能走通next = cur;next._y = cur._y + 1;if (IsNextPass(m, cur, next) == 1){PushStack(*s, next);m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;//PrintMaze(m);continue;}//下:看當前節點的下一個節點能不能走通next = cur;next._x = cur._x + 1;if (IsNextPass(m, cur, next) == 1){PushStack(*s, next);m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;//PrintMaze(m);continue;}//走到這里說明當前節點的四個方向都走不通,進行回溯,看前一個節點未被遍歷的方向是否還能走通Position tmp;PopStack(*s, tmp);}return 0; }int main() {int map[ROW][COL] = { //用二維數組描繪迷宮:1 代表通路,0 代表墻0, 0, 1, 0, 0, 0,0, 0, 1, 1, 1, 0,0, 0, 1, 0, 0, 0,0, 1, 1, 1, 1, 0,0, 0, 1, 0, 1, 0,0, 0, 0, 0, 1, 0};Maze m;Position enter; //迷宮入口enter._x = 0;enter._y = 2;InitMaze(&m, map);PrintMaze(&m);//system("pause");//exit(0);SqStack s; //定義棧,保存已走過的坐標軌跡,便于回溯InitStack(s); //棧的初始int ret = PassMaze(&m, enter, &s); //使用棧和回溯法解開迷宮if (ret) {printf("恭喜你!終于找到了出口~\n");}else {printf("不是我笨!實在沒有出口~\n");}PrintMaze(&m);system("pause");return 0; }

參考資料:

  • C/C++從入門到精通-高級程序員之路【奇牛學院】
  • 總結

    以上是生活随笔為你收集整理的使用栈求解迷宫问题的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。