#include <iostream>
#include <vector>
#include <queue>
#include <string>
using namespace std;
const int MAX_VERTEX_NUM = 7;
class Graph {
private:
vector<string> vertex;
vector<vector<int>> adjList;
vector<bool> visited;
public:
Graph() {
vertex.resize(MAX_VERTEX_NUM);
adjList.resize(MAX_VERTEX_NUM);
visited.resize(MAX_VERTEX_NUM, false);
}
void initGraph() {
vertex[0] = "A";
vertex[1] = "B";
vertex[2] = "C";
vertex[3] = "D";
vertex[4] = "E";
vertex[5] = "F";
vertex[6] = "G";
adjList[0] = {1, 2};
adjList[1] = {0, 3};
adjList[2] = {0, 4};
adjList[3] = {1, 5};
adjList[4] = {2, 5};
adjList[5] = {3, 4, 6};
adjList[6] = {5};
}
void BFS(int start, int end) {
queue<vector<int>> q;
q.push({start});
visited[start] = true;
while (!q.empty()) {
vector<int> currentPath = q.front();
q.pop();
int current = currentPath.back();
if (current == end) {
cout << "找到路径: ";
for (int node : currentPath) {
cout << vertex[node] << " ";
}
cout << endl;
continue;
}
for (int neighbor : adjList[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
vector<int> newPath = currentPath;
newPath.push_back(neighbor);
q.push(newPath);
}
}
}
fill(visited.begin(), visited.end(), false);
}
int main() {
Graph graph;
graph.initGraph();
graph.BFS(0, 6);
return 0;
}