-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtorn2pieces.java
116 lines (102 loc) · 3.63 KB
/
torn2pieces.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.util.*;
public class torn2pieces {
private static String res = "";
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(in.readLine());
Graph g = new Graph();
ArrayList<String> exists = new ArrayList<String>();
for(int i = 0; i < n; i++) {
String line = in.readLine();
String name = line.substring(0, line.indexOf(" "));
boolean contains = exists.contains(name);
Node node;
if(!contains) {
exists.add(name);
node = new Node(name);
}
else
node = g.getNode(name);
String[] temp = line.substring(line.indexOf(" ") + 1).split(" ");
for(int j = 0; j < temp.length; j++) {
if(exists.contains(temp[j])) {
node.neighbors.add(g.getNode(temp[j]));
g.getNode(temp[j]).neighbors.add(node);
}
else {
exists.add(temp[j]);
g.addNode(temp[j], new Node(temp[j]));
node.neighbors.add(g.getNode(temp[j]));
g.getNode(temp[j]).neighbors.add(node);
}
}
g.addNode(name, node);
}
StringTokenizer st = new StringTokenizer(in.readLine());
String start = st.nextToken();
String end = st.nextToken();
if(g.graph.get(start) == null && g.graph.get(end) == null)
out.println("no route found");
else {
List<Node> res = BFS(g.getNode(start), g.getNode(end));
out.println(res.size() > 1 ? res.toString().replace("[", "").replace("]", "").replace(",", "") : "no route found");
}
out.close();
}
static class Graph {
Map<String, Node> graph;
public Graph() {
graph = new HashMap<String, Node>();
}
public void addNode(String name, Node n) {
graph.put(name, n);
}
public Node getNode(String name) {
return graph.get(name);
}
}
static class Node {
// public Node[] neighbors;
public ArrayList<Node> neighbors;
String name;
boolean visited;
public Node(String name) {
this.name = name;
this.visited = false;
neighbors = new ArrayList<Node>();
}
public String toString() {
return name;
}
public boolean equals(Object o) {
return ((Node) o).name.equals(name);
}
}
public static List<Node> BFS(Node start, Node finish) {
List<Node> res = new LinkedList<Node>();
Queue<Node> queue = new LinkedList<Node>();
Map<Node, Node> prev = new HashMap<Node, Node>();
start.visited = true;
queue.add(start);
while(!queue.isEmpty()) {
Node v = queue.remove();
if(v.equals(finish))
break;
else {
for(Node w : v.neighbors) {
if(!w.visited) {
queue.add(w);
w.visited = true;
prev.put(w, v);
}
}
}
}
for(Node node = finish; node != null; node = prev.get(node))
res.add(node);
Collections.reverse(res);
return res;
}
}