-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPesquisaLargura.java
47 lines (39 loc) · 1.44 KB
/
PesquisaLargura.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// BUILD: javac PesquisaLargura.java
// EXECUTE: java PesquisaLargura
import java.util.*;
public class PesquisaLargura {
static Map<String, List<String>> graph = new HashMap<>();
static {
graph.put("voce", Arrays.asList("Alice", "Bob", "Claire"));
graph.put("Bob", Arrays.asList("Anuj", "Peggy"));
graph.put("Alice", Arrays.asList("Peggy"));
graph.put("Claire", Arrays.asList("Thom", "Jonny"));
graph.put("Anuj", new ArrayList<>());
graph.put("Peggy", new ArrayList<>());
graph.put("Thom", new ArrayList<>());
graph.put("Jonny", new ArrayList<>());
}
static boolean isSalesPerson(String name) {
return name.endsWith("m");
}
static boolean search(String name) {
Queue<String> searchQueue = new LinkedList<>(graph.get(name));
List<String> searched = new ArrayList<>();
while (!searchQueue.isEmpty()) {
String person = searchQueue.poll();
if (!searched.contains(person)) {
if (isSalesPerson(person)) {
System.out.println(person + " é um vendedor de Manga!");
return true;
} else {
searchQueue.addAll(graph.get(person));
searched.add(person);
}
}
}
return false;
}
public static void main(String[] args) {
search("voce");
}
}