#include<iostream>
#include<queue>
using namespace std;
bool vis[6];
int g[6][6] = {
{0,1,0,0,0,0},
{1,0,1,0,1,0},
{0,1,0,1,0,0},
{0,0,1,0,0,0},
{0,1,0,0,0,1},
{0,0,0,0,1,0}
};
void bfs() {
queue<int> q;
q.push(0); vis[0] = 1;
while (!q.empty())
{
int cut = q.front(); q.pop();
cout << char(cut + 'A');
for (int i = 0; i < 6; i++)
{
if (g[cut][i] == 1 && !vis[i]) {
q.push(i); vis[i] = 1;
}
}
}
}
int main()
{
bfs();
return 0;
}
图的广度优先搜索
最新推荐文章于 2024-08-31 15:48:32 发布