Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f75fc60

Browse files
authored
Create 6.3.7Kevin Bacon的六度.md
1 parent 4bd3a7e commit f75fc60

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

‎6.3.7Kevin Bacon的六度.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
- 问题:有个游戏叫kevin bacon的六度,与寻求最短路径有关,路径一端为kevin bacon,另一端为随机选择的演员,如果两个演员出现在同一部电影中,则两者相连。游戏的目标是用尽可能少的链接将特定的演员连接到kevin bacon。
2+
- 思路:
3+
- 设计数据结构:
4+
- 一个显而易见的观点是:在图中的每一个节点对应一个演员。如果电影也用节点表示,就会使数据结构变得更加复杂。
5+
- 如果用边来表示电影,一部影片就将由许多条边来表示,这样严重增加了图中边的总数,也难以提取电影信息。
6+
- 合理的办法是使用对象来表示每个节点。定义一个数据结构叫ActorGraph类,其中,字符串型的actorName表示演员名,vector型的linkActor表示和本演员同时出现在同一个电影中的其他演员。
7+
8+
```cpp
9+
class ActorGraph{
10+
public:
11+
vector<ActorGraph> linkActor;// 方便起见,将linkActor声明成公共的。在面试时,需要跟面试官说明这是为了方便起见,而不是你不能设计出一个良好的数据结构
12+
private:
13+
string actorName;
14+
};
15+
```
16+
17+
- BFS思想:
18+
- 声明一个map<ActorGraph, bool>型的visitedMap,当每次访问到图中的某一个节点时,就标记该节点已经访问过了。具体方法是向map插入一个新键 <ActorGraph, true>
19+
- 声明一个queue<ActorGraph>型的队列actorQueue,当每次访问到图里的某一个节点时,先使用map判断该节点是否访问过,如果没有,就加入队列。
20+
- 声明一个vector<ActorGraph>型的shortestPath,保存每次访问的路径
21+
22+
在一个循环中,重复以下步骤,直到找到目标演员或队列为空:
23+
24+
- 从队列中取出一个演员节点(当前节点)。
25+
- 检查当前节点是否是目标演员:
26+
- 如果是目标演员,表示找到了最短路径,结束算法。
27+
- 如果不是目标演员,继续下面的步骤。
28+
- 遍历当前演员节点的所有链接演员(共同出演过同一部电影的演员):
29+
- 对于每个链接演员,检查是否已经访问过。如果没有访问过,将其标记为已访问并加入队列中。
30+
- 记录链接演员与当前演员之间的关系,以便后续回溯路径。
31+
32+
```cpp
33+
#include <iostream>
34+
#include <vector>
35+
#include <queue>
36+
#include <map>
37+
using namespace std;
38+
39+
class ActorGraph{
40+
public:
41+
ActorGraph(const string& name) : actorName(name) {};
42+
~ActorGraph() {};
43+
void AddLink(ActorGraph& actor);
44+
const string& GetName() const;
45+
int FindActor(const string& name) const;
46+
47+
vector<ActorGraph> linkActor;
48+
49+
bool operator<(const ActorGraph& ac) const {
50+
return this->actorName < ac.GetName();
51+
}
52+
53+
private:
54+
string actorName;
55+
};
56+
57+
const string& ActorGraph::GetName() const{
58+
return this->actorName;
59+
}
60+
61+
int ActorGraph::FindActor(const string& name) const{
62+
for(int i = 0; i < this->linkActor.size(); i++){
63+
if(this->linkActor[i].GetName() == name){
64+
return i;
65+
}
66+
}
67+
return -1;
68+
}
69+
70+
void ActorGraph::AddLink(ActorGraph& ag){
71+
int findFlag = this->FindActor(ag.GetName());
72+
if(findFlag != -1){
73+
return;
74+
}
75+
this->linkActor.push_back(ag);
76+
ag.AddLink(*this);
77+
}
78+
79+
void BFS(ActorGraph endActor, queue<ActorGraph>& que, map<ActorGraph, bool>& vmap, vector<ActorGraph>& aPath){
80+
while(!que.empty()){
81+
ActorGraph actor = que.front();
82+
if(actor.GetName() == endActor.GetName()){
83+
break;
84+
}
85+
for(int i = 0; i < actor.linkActor.size(); i++){
86+
ActorGraph linkActor = actor.linkActor[i];
87+
if(vmap.find(linkActor) == vmap.end()){
88+
vmap[linkActor] = true;
89+
que.push(linkActor);
90+
aPath.push_back(linkActor);
91+
}
92+
}
93+
que.pop();
94+
}
95+
}
96+
97+
int main(){
98+
// 创建演员对象
99+
ActorGraph KevinBacon("Kevin Bacon");
100+
ActorGraph ThePhantomOfTheOpera("ThePhantomOfTheOpera");
101+
ActorGraph GerardButler("Gerard Butler");
102+
ActorGraph EmmyRossum("Emmy Rossum");
103+
ActorGraph PatrickWilson("Patrick Wilson");
104+
105+
// 建立连接关系
106+
KevinBacon.AddLink(ThePhantomOfTheOpera);
107+
ThePhantomOfTheOpera.AddLink(GerardButler);
108+
ThePhantomOfTheOpera.AddLink(EmmyRossum);
109+
ThePhantomOfTheOpera.AddLink(PatrickWilson);
110+
GerardButler.AddLink(EmmyRossum);
111+
GerardButler.AddLink(PatrickWilson);
112+
GerardButler.AddLink(KevinBacon);
113+
114+
queue<ActorGraph> actorQueue;
115+
map<ActorGraph, bool> visitedMap;
116+
vector<ActorGraph> shortestPath;
117+
118+
// 将Kevin Bacon作为起始演员
119+
actorQueue.push(KevinBacon);
120+
visitedMap[KevinBacon] = true;
121+
122+
// 调用BFS函数来查找最短路径
123+
BFS(GerardButler, actorQueue, visitedMap, shortestPath);
124+
125+
// 输出最短路径
126+
cout << "Shortest Path: ";
127+
for (const ActorGraph& actor : shortestPath) {
128+
cout << actor.GetName() << " -> ";
129+
}
130+
cout << endl;
131+
132+
}
133+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /