# 广度优先搜索算法（go）

```package main

import "fmt"

func main() {
friendCircle := createFriendCircle()
}

func personIsSeller(name string) bool {
return name[len(name)-1] == 'y'
}

func createFriendCircle() map[string][]string {
fc := make(map[string][]string)
fc["you"] = []string{"alice", "bob", "claire"}
fc["bob"] = []string{"anuj", "peggy"}
fc["alice"] = []string{"peggy"}
fc["claire"] = []string{"thom", "jonny"}
fc["anuj"] = []string{}
fc["peggy"] = []string{}
fc["thom"] = []string{}
fc["jonny"] = []string{}
return fc
}

searchList := friendCircle["you"]
if len(searchList) == 0 {
return false
}

searched := make(map[string]bool)
for {
person := searchList[0]
searchList = searchList[1:]
_, found := searched[person]
if !found {
if personIsSeller(person) {
fmt.Println(person + " is a seller!")
return true
} else {
searchList = append(searchList, friendCircle[person]...)
searched[person] = true
}
}

if len(searchList) == 0 {
break
}
}

return false
}

/*
peggy is a seller!
*/```

69 篇文章30 人订阅

0 条评论

## 相关文章

851

3195

4816

3385

2688

5143

4279

1535

871

### 庖丁解牛切割数据！| 【SAS Says·扩展篇】

【SAS Says·扩展篇】庖丁解牛割数据！ | 3. call PRXSUBSTR () 0. 前集回顾 1. 新的问题 2. 初识 PRXSUBSTR() ...

3326