迷宮求解 C

2021-08-02 19:14:42 字數 2082 閱讀 3514

標籤(空格分隔): 演算法**

乙個迷宮,由n行m列的單元格組成(0 < n,m <= 50),每個空格要麼為障礙物,要麼為空格,求一條從迷宮起點(1,1)到目標地點(小哈的位置)的最短路徑。

通過起點去向下乙個位置探索,將下一步可以到達的位置加入佇列,並標記已經走過。然後從佇列中依次取點,再向下一步可以到達的位置探索,再加入佇列,直到到達小哈的位置為止。

//

// main.cpp

// maze_dfs

//// created by jtusta on 2017/6/19.

//#include

#include

using

namespace

std;

int n,m,p,q,minx=999999;

int a[51][51],book[51][51];

void dfs(int x,int y,int step),,,};

int tx,ty,k;

if(x==p && y==q)

for(k=0;k<4;k++)

}return;

}int main()

/* * 測試資料

5 40 0 1 0

0 0 0 0

0 0 1 0

0 1 0 0

0 0 0 1

1 1 4 3

*/

540

0100

0000

0100

1000

0011

1437

program ended with

exit code: 0

//

// main.cpp

// maze_bfs

//// created by jtusta on 2017/6/19.

//#include

#include

using

namespace

std;

struct note

;int main() ,book[51][51]=;

int next[4][2]=,,,}; //向四個方向走的陣列

int head,tail;

int i,j,k,n,m,startx,starty,p,q,tx,ty,flag;

cin>>n>>m;

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

cin>>a[i][j];

cin>>startx>>starty>>p>>q;

head=1; //佇列初始化

tail=1;

que[tail].x=startx; //往佇列插入迷宮入口座標

que[tail].y=starty;

que[tail].f=0;

que[tail].s=0;

tail++;

book[startx][starty]=1;

flag=0;

while(headfor(k=0;k<4;k++)

if(tx==p&&ty==q)

}if(flag==1)

break;

head++; //此地方不能忘記,當乙個點拓展結束,head++才能對後面的點再進行拓展

}//列印佇列中末尾最後乙個點(目標點)的步數

//注意tail是指向佇列隊尾(即最後一位)的下乙個位置,所以這需要-1

cout

<1].s/* 測試資料

5 40 0 1 0

0 0 0 0

0 0 1 0

0 1 0 0

0 0 0 1

1 1 4 3

*/

540

0100

0000

0100

1000

0011

1437

program ended with

exit code: 0

C 迷宮求解詳解 棧

相信大家都應該聽過棧吧,一直想利用棧來實現乙個演算法,最近有點空,就利用棧的先進後出的特性來完成迷宮求的問題,下面將詳細介紹棧的實現和迷宮求解的過程,可以很好的學習棧的使用。棧有兩種實現方法,一種是順序,即陣列形式,一種是線性,即鍊錶形式,個人還是比較喜歡鍊錶形式實現棧的基本功能。首先弄乙個簡單的迷...

C 棧實現迷宮求解

給出乙個迷宮,求解從路口到出口的全部路徑是乙個經典問題,c 實現 如下 algrithm practice.cpp 定義控制台應用程式的入口點。include stdafx.h using namespace std define d 10 迷宮的維度 include includeint maze...

AI 隨機迷宮 迷宮求解

本文記錄了,人工智慧中簡單的搜尋策略中的路徑搜尋策略中的a 演算法,來實現迷宮尋路的問題.這只是一次本人的課外作業 完整的程式原始碼已經傳送到我的git.這裡只記錄了我的思路和感想以及收穫.產生隨機迷宮 迷宮求解沒有迷宮怎麼可以呢.而本人是個懶人,每次都要手動輸入迷宮,重複性的工作讓我很不爽.你可以...