-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_12952.java
More file actions
36 lines (32 loc) · 837 Bytes
/
_12952.java
File metadata and controls
36 lines (32 loc) · 837 Bytes
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
class Solution {
public int solution(int n) {
init(n);
BT(0,0);
return answer;
}
static int N, answer;
static boolean[] checkC, checkD1, checkD2;
public static void init(int n){
N = n;
answer = 0;
checkC = new boolean[N];
checkD1 = new boolean[2*N-1];
checkD2 = new boolean[2*N-1];
}
public static void BT(int r, int c){
if(r==N){
answer++;
return;
}
int d1 = r+c;
int d2 = N-1-r+c;
if(!checkC[c] && !checkD1[d1] && !checkD2[d2]){
checkC[c] = checkD1[d1] = checkD2[d2] = true;
BT(r+1,0);
checkC[c] = checkD1[d1] = checkD2[d2] = false;
}
if(c+1<N){
BT(r,c+1);
}
}
}