-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSlashMaze.java
124 lines (115 loc) · 4.14 KB
/
SlashMaze.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
117
118
119
120
121
122
123
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SlashMaze {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
private static int[] find(Map<Integer, List<Integer>> graph) {
int count = 0;
int max = 0;
Set<Integer> visited = new HashSet<>();
while (true) {
int[] m = new int[] { 0, 0 };
Integer src = graph.keySet().stream()
.filter(x -> !visited.contains(x))
.findFirst().orElse(Integer.MIN_VALUE);
if (src == Integer.MIN_VALUE) {
break;
}
find(graph, visited, src, m);
count += m[0];
max = Math.max(max, m[1]);
}
return new int[] { count, max / 2 };
}
private static void find(Map<Integer, List<Integer>> graph,
Set<Integer> visited, int parent, int m[]) {
Deque<Integer> stack = new ArrayDeque<>();
Map<Integer, Integer> depths = new HashMap<>();
int src = parent;
stack.push(src);
depths.put(src, 0);
while (!stack.isEmpty()) {
src = stack.pop();
int depth = depths.get(src);
if (!visited.contains(src)) {
visited.add(src);
for (int adj : graph.get(src)) {
if (adj == parent && depth > 2) {
m[0] += 1;
m[1] = Math.max(m[1], depth + 1);
}
stack.push(adj);
depths.put(adj, depth + 1);
}
}
}
}
private static List<Integer> parseLine(String line) {
return stream(line.trim().split(" "))
.filter(x -> !x.equals(""))
.map(Integer::parseInt)
.collect(toList());
}
public static void add(Map<Integer, List<Integer>> graph, int src,
int dst) {
if (!graph.containsKey(src)) {
graph.put(src, new ArrayList<>());
}
if (!graph.containsKey(dst)) {
graph.put(dst, new ArrayList<>());
}
graph.get(src).add(dst);
graph.get(dst).add(src);
}
public static void main(String[] args) throws IOException {
String currentLine;
int scenario = 1;
while ((currentLine = reader.readLine()) != null &&
!currentLine.trim().isEmpty()) {
List<Integer> wh = parseLine(currentLine);
if (wh.get(0) == 0 && wh.get(1) == 0) {
break;
}
int node = 0;
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int i = 0; i < wh.get(1); ++i) {
currentLine = reader.readLine().trim();
for (char c : currentLine.toCharArray()) {
if (c == '/') {
add(graph, node, node + 1);
add(graph, node + 2, node + 3);
} else if (c == '\\') {
add(graph, node, node + 3);
add(graph, node + 1, node + 2);
}
add(graph, node, node - 2);
int nodeBelow = node + (wh.get(0) * 4) + 1;
add(graph, node + 3, nodeBelow);
node += 4;
}
}
int[] result = find(graph);
System.out.println("Maze #" + scenario + ":");
if (result[0] > 0) {
System.out.println(
result[0] + " Cycles; the longest has length " +
result[1] + ".");
} else {
System.out.println("There are no cycles.");
}
scenario++;
System.out.println();
}
}
}