#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100;
int arr[N][N];
int visited[N];
int n;
string s;
void bfs(int begin)
{
queue<int> q;
q.push(begin);
while (q.size())
{
int front = q.front();
cout << s[front];
visited[front] = 1;
q.pop();
for (int i = 0; i < n; i++)
{
if (arr[front][i] != 0 && !visited[i])
{
q.push(i);
visited[i] = 1;
}
}
}
}
int main()
{
cin >> n;
cin >> s;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
char e;
cin >> e;
int begin = s.find(e);
bfs(begin);
return 0;
}
swust oj 1069: 图的按录入顺序广度优先搜索
于 2024-05-17 21:17:44 首次发布