廣度搜尋bfs (杭電)

2021-10-22 07:35:13 字數 1355 閱讀 7477

1.優先佇列 按時間優先 時間小的先出佇列 可以使英雄更快的救出公主

2.最後輸出的時候 採用遞迴

#include

#include

#include

#include

#include

using

namespace std;

struct points};

char map[

100]

[100];

points flag[

100]

[100];

//它的count是map陣列的每個地方的時間值,x和y是它上一步的位置

int direction[2]

[4]=

;//右,下,左,上

int n,m;

intbfs

(int

&count)

points start,tmp;

start.x=

0;start.y=

0;start.count=0;

qu.push

(start)

;while

(!qu.

empty()

)for

(int i=

0;i<

4;i++

) start.x=tx;start.y=ty;start.count=tmp.count+1;

flag[ty]

[tx]

=tmp;

flag[ty]

[tx]

.count=0;

if(map[ty]

[tx]

>

'0'&& map[ty]

[tx]

<

'10'

) qu.

push

(start)

; map[ty]

[tx]

='x'

;//這個格仔走過了就封掉,因為如果再走這個格仔時間會花更久 }}

return0;

}void

display

(int count,

int x,

int y)

intmain()

}int count=0;

memset

(flag,0,

sizeof

(flag));

int result=

bfs(count);if

(result)

else

cout<<

"finish"

<}return0;

}

廣度搜尋bfs

廣度搜尋在acm中應用十分廣泛,英文簡寫是bfs breadth first search 下面先看一下例子 在乙個4 4的矩形中,有一些位置設定有障礙,要求從 1,1 走到 4,4 求最短距離。分析 假設沒有任何障礙,我們可以走的路線如下 起點為 1,1 假設這一步是第一步可以到達的位置 然後它可...

BFS廣度優先搜尋

廣度優先搜尋,利用佇列實現,結束標誌是隊列為空的時候 承接dfs的演算法實現的講例,對於迷宮問題我們也可以採取廣度優先搜尋實現 include iostream include cstdio include cstdlib using namespace std int map 55 55 int ...

bfs廣度優先搜尋

這一課我們來學習圖的另一種遍歷方法 廣度優先搜尋 breadth first search,簡稱 bfs 這是一種連通圖的常用遍歷策略,通常用於求起點到各點的最短路徑,以及求兩點之間的最優路徑等問題。首先我們先來看看廣度優先搜尋的具體方法吧 對於乙個連通圖,我們假設一開始所有頂點均未被訪問,廣度優先...