广度优先搜索
1.使用图来建立问题模型
2.使用广度优先搜索解决问题
from collections import deque
def person_is_seller(name):
return name[-1] == "m" # 检查人的姓名是否以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")