1 Star 2 Fork 1

大象乔布斯/AlgorithmDiagram

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Link_SearchQueue.java 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
link98 提交于 2018-01-02 17:32 +08:00 . add Java version and result photo
public class searchQueue{
//使用二维数组表示有向图
static String[] graphName = {"you","alice","bob","claire","anuj","peggy","thom","jonny"};
static String[][] graph = {
{"you","alice"},
{"you","bob"},
{"you","claire"},
{"bob","anuj"},
{"bob","peggy"},
{"alice","peggy"},
{"claire","thom"},
{"claire","jonny"},
};
public static boolean Search(String name){
Queue<String> searchQueue = new LinkedList<>();
Queue<String> searched = new LinkedList<>();
for (int i = 0; i < graphName.length; i++)
searchQueue.add(graphName[i]);
while (!searchQueue.isEmpty()) {
String person = searchQueue.poll();
if(!searched.contains(person)) {
if(personIsSeller(person)) {
System.out.println(person + " is a mango seller!");
return true;
}else {
for (int i = 0; i < graph.length; i++) {
if (graph[i][0].equals(person))
searchQueue.add(graph[i][1]);
}
searched.add(person);
}
}
}
return false;
}
public static boolean personIsSeller(String name){
return name.charAt(name.length()-1) == 'm';
}
public static void main(String[] args) {
System.out.println(Search("you"));
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python-deathfans/AlgorithmDiagram.git
git@gitee.com:python-deathfans/AlgorithmDiagram.git
python-deathfans
AlgorithmDiagram
AlgorithmDiagram
master

搜索帮助