-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiggerOctaves.java
97 lines (86 loc) · 2.23 KB
/
DiggerOctaves.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
/**
* https://www.spoj.com/problems/UCI2009D/ #backtracking #bit-manipulation -> bit manipulation: ->
* 8x8 -> 64bit.
*/
import java.util.Scanner;
import java.util.TreeSet;
class DiggerOctaves {
static int maxLen = 8;
static int[] dx = {0, 0, -1, 1};
static int[] dy = {1, -1, 0, 0};
public static boolean check(long bitSet, int n, int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= n) return false;
if (((bitSet >> ((x * n) + y)) & 1) == 1) return false;
return true;
}
public static void calculate(
long bitSet, int n, int startX, int startY, int len, TreeSet<Long> resultSet) {
// success
if (len == 0) {
resultSet.add(bitSet);
return;
}
// try
for (int i = 0; i < dx.length; i++) {
int x = startX + dx[i];
int y = startY + dy[i];
if (check(bitSet, n, x, y)) {
int k = (x * n) + y;
bitSet ^= 1l << k;
calculate(bitSet, n, x, y, len - 1, resultSet);
bitSet ^= 1l << k;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
for (int t = 0; t < testcases; t++) {
int n = sc.nextInt();
String row;
long bitSet = 0;
for (int i = 0; i < n; i++) {
row = sc.next();
for (int j = 0; j < row.length(); j++) {
if (row.charAt(j) != 'X') {
bitSet ^= 1l << (i * n + j);
}
}
}
// special case
if (n < 3) {
System.out.println(0);
continue;
}
// calculate
TreeSet<Long> resultSet = new TreeSet<>();
int len = maxLen;
int k;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
k = (i * n) + j;
if (((bitSet >> k) & 1) == 0) {
bitSet ^= 1l << k;
calculate(bitSet, n, i, j, len - 1, resultSet);
bitSet ^= 1l << k;
}
}
}
System.out.println(resultSet.size());
}
}
}
class Point implements Comparable<Point> {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Point other) {
if (x != other.x) {
return x - other.x;
} else {
return y - other.y;
}
}
}