from collections import deque
# 名字最后一个为m字母的为芒果销售商
def person_is_seller(name):
return name[-1] == 'm'
graph = {}
graph["you"] = ["alice", "bob", "claire"]
graph["bob"] = ["anuj", "peggy"]
graph["alice"] = ["peggy"]
graph["claire"] = ["thom", "jonny"]
graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
graph["jonny"] = []
def search(name):
search_queue = deque()
search_queue += graph[name]
# 这个数组用于记录检查过的人
searched = []
while search_queue:
person = search_queue.popleft()
# 仅当这个人没有检查过才检查
if person not in searched:
if person_is_seller(person):
print(person + " is a mango seller!")
return True
else:
# 把当前用户的邻居加入队列
search_queue += graph[person]
# 将这个人标记为检查过
searched.append(person)
return False
search("you")
6.广度优先搜索
最新推荐文章于 2020-05-19 23:44:41 发布