迷宮問題(四方向)c

2021-10-25 12:48:51 字數 1636 閱讀 3357

總結四個方向的迷宮問題

定義乙個二維陣列:

int maze[5][5] = point;

int num_step =0;

stack result_path;

int maze[7]

[7]=

,,,,

,,};

//在周圍新增一圈不可達的位置,避免邊界處理

int mark[7]

[7]=

;//標記走過的位置

point mov[4]

=,,,

};bool

mazepath

(int row,

int col,

int x,

int y)

flag =

true

;for

(int i=

0;i<

4;i++)}

if(flag)

}return flag;

}void

printpath

(int row,

int col)

while

(!print_path.

empty()

) cout << endl;

cout <<

"最短路徑需要"

<< num_step <<

"步"<< endl;

}int

main()

else

return0;

}八個方向的迷宮問題見另外一篇部落格

新增鏈結描述

**如下:

#include

#include

using

namespace std;

typedef

struct

point;

int maze[5]

[5]=

,,,,

};//定義迷宮

int mark[5]

[5]=

;//用矩陣表示已經走過的位置,初始化為全零

point mov[4]

=,,,

};//移動方向

point pre_point[10]

[10];

//儲存前乙個位置

void

bfs(

int row,

int col,

int x,

int y)

for(

int i=

0;i<

4;i++)}

}}void

print_path

(point cur_point,

int x,

int y)

print_path

(pre_point[cur_point.x]

[cur_point.y]

,x,y)

;//遞迴

cout <<

"("<< cur_point.x <<

" , "

<")"<<

" ";

cout<}int

main()

以上方式還適用於 任意起止點 路徑,通過改變起點和終點的位置即可

貪吃蛇四方向走位

貪吃蛇四方向走位 本節取消讓貪吃蛇只會向右走,讓新節點根據座標來確定。運用了全域性變數,封裝函式,巨集定義,switchcase 本節錯誤 刪除了尾插的演算法,導致開始出現bug 乙個bug,撞到hang 1的時候會重置 用絕對值解決不合理走位 本節將上下,左右 的巨集定義改為互相的絕對值 封裝了t...

迷宮尋路(八方向)

使用動態陣列儲存迷宮和迷宮的路徑,1表示牆0表示可走。感覺寫法有點 include using namespace std int stepcomplete void cin maze int a,int row,int column void print int maze path,int h,i...

八方向迷宮尋路的全部路徑

八方向迷宮,可尋出全部路徑 最外層需要全部放1 寫的比較粗糙,日後改進 include using namespace std int stepcomplete int ans 0 void cin maze int a,int row,int column void print int maze ...