2020 11 6 用棧實現迷宮求解

2021-10-10 07:12:33 字數 1518 閱讀 6450

#include#include//包含system("pause")、system("cls")函式

#include//包含srand、rand函式

using namespace std;

#define mazetype int**//迷宮二維陣列的元素為int型

typedef struct

posttype;//位置資訊

typedef struct

selemtype;//存放在鏈棧的元素型別

//下面建立的鏈棧儲存結構為迷宮問題的求解服務

typedef struct snode

snode,* slink;

typedef struct

linkstack;//鏈棧

bool init_linkstack(linkstack& s)//初始化鏈棧

bool isempty_linkstack(linkstack s)//鏈棧為空,返回true;否則返回false

selemtype gettop(linkstack s)//返回棧頂元素

bool push(linkstack& s,selemtype e)//壓棧

bool pop(linkstack& s,selemtype& e)//出棧

ostream& operator<<(ostream& os,selemtype e)//該函式過載cout<";

}}bool creat_maze(mazetype& maze,int n)//初始化迷宮 maze[i][j]==1表示該位置(i,j)是牆

for(int i=0;i!=n;i++)

for(int i=1;i!=n-1;i++)

cout<<"檢驗迷宮\n";

for(int i=0;i!=n;i++)

coutelse

if(e.di<4)}}

}while(!isempty_linkstack(s));

return false;

}void showdynamiceffect(mazetype maze,int n,linkstack s,posttype end)//展示動態路徑

while(!isempty_linkstack(s1))

else if(e1.seat.y==e.seat.y)

for(int i=0;i!=n;i++)

}cout<>(istream& is,posttype& e)//該函式過載cin>>posttype型別資料

int main()

posttype start,end;

cout<<"請輸入迷宮的起點:";cin>>start;

cout<<"請輸入迷宮的終點:";cin>>end;

if(mathpath(maze,start,end,s))

else

cout<<"迷宮無解\n";

return 0;

}

// 讀者自行測試即可

用棧求解迷宮問題

問題描述 二維陣列mg m n 它表示乙個m n的迷宮,其中的1表示牆壁,0表示可以走的路,只能橫著走或豎著走,不能斜著走,要求程式設計序找出從左上角到右下角的路線。include define maxsize 100 define m 8 define n 8 struct st maxsize ...

棧實現迷宮求解問題

總體感觸是 不要著急,一步一步來,問題很容易解決的。首先是要實現乙個迷宮的地圖。明確如何儲存地圖,用vector實現二維陣列,每個元素代表地圖的乙個格仔。需要儲存哪些資訊。一張地圖的某乙個方格需要標示 能否通過,是否走過了。94 struct point 位置資訊可用postype儲存。就是 str...

迷宮求解 棧的實現

一 題目及分析 二 思路 迷宮 迷宮的檔案儲存方式 三 include include define length 10 define width 10 define start hang 1 從0行開始計算row define start lie 1 從0列開始column define end ...