解法:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int arr[100][100];
string vertex;
void dfs(vector<int>& a,int u) {
a[u] = 1;
cout << vertex[u];
for (int i = 0; i < a.size(); i++) {
if (arr[u][i] && !a[i]) {
dfs(a, i);
}
}
}
int main() {
int n;
cin >> n;
vector<int> vis(n, 0);
cin >> vertex;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
char x;
cin >> x;
int pos;
for (pos = 0; pos < vertex.size(); pos++) {
if (x == vertex[pos])
break;
}
dfs(vis, pos);
return 0;
}