?? p272.cpp
字號(hào):
#include "iostream.h"
#include "queue.h"
#include "p267E.cpp"
template <class NameType, class DistType>
void Graph<NameType, DistType>::BFS ( int v ) {
//從頂點(diǎn)v出發(fā), 以廣度優(yōu)先的次序橫向搜索圖, 算法中使用了一個(gè)隊(duì)列。
int *visited = new int[NumVertices]; //visited記錄頂點(diǎn)是否訪問過
for ( int i=0; i<NumVertices; i++ ) visited[i] = 0; //初始化
cout << GetValue (v) << ' ';
visited[v] = 1; //首先訪問頂點(diǎn)v, 做已訪問標(biāo)記
Queue<int> q; //q是實(shí)現(xiàn)分層訪問的隊(duì)列
q.EnQueue (v); //頂點(diǎn)v進(jìn)隊(duì)列
while ( !q.IsEmpty ( ) ) {
v = q.DeQueue ( ); //從隊(duì)列中退出頂點(diǎn)v
int w = GetFirstNeighbor (v); //找頂點(diǎn)v的第一個(gè)鄰接頂點(diǎn)
while ( w != -1 ) { //w是v的鄰接頂點(diǎn)
if ( !visited[w] ) { //若未訪問過
cout << GetValue (w) << ' '; visited[w] = 1; //訪問頂點(diǎn)w
q.EnQueue (w); //頂點(diǎn)w進(jìn)隊(duì)列
}
w = GetNextNeighbor (v, w); //找頂點(diǎn)v的下一個(gè)鄰接頂點(diǎn)
}
}
delete [ ] visited;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -