-
Notifications
You must be signed in to change notification settings - Fork 0
/
NudnikPhotographer.java
62 lines (41 loc) · 1.66 KB
/
NudnikPhotographer.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
import java.util.*;
import java.io.*;
public class NudnikPhotographer {
static Map<String, Integer> cache;
public static void main(String[] args) {
Scanner in = new Scanner(System.in).useLocale(Locale.US);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
cache = new HashMap<>();
System.out.println("" + dfs(1, n, 1L, 1));
}
public static int dfs(int root, int len, long visited, int cnt) {
if (cnt > len) {
return 1;
}
String key = root + "_" + visited;
if (cache.containsKey(key)) {
System.out.println("hit "+key);
return cache.get(key);
}
int result = 0;
for (int i = 1; i <= 2; i++) {
int child = root - i;
if (child > 1 && (visited & (1L << child)) == 0) {
System.out.println("Going from "+root+" to "+child);
visited |= (1L << child);
result += dfs(child, len, visited, cnt + 1);
visited &= ~(1L << child);
}
child = root + i;
if (child <= len && (visited & (1L << child)) == 0 ) {
System.out.println("Going from "+root+" to "+child);
visited |= (1L << child);
result += dfs(child, len, visited, cnt +1);
visited &= ~(1L << child);
}
}
cache.put(key,result);
return result;
}
}