forked from gojung/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_2806.java
More file actions
40 lines (39 loc) · 1.25 KB
/
java_2806.java
File metadata and controls
40 lines (39 loc) · 1.25 KB
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
import java.util.*;
import java.io.*;
public class java_2806 {
static int N;
static int answer;
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("test.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for(int t = 1; t <= T; t++){
N = Integer.parseInt(br.readLine());
answer = 0;
for(int i = 1; i <= N; i++) {
int[] col = new int[N+1];
col[1] = i;
setQueen(col, 1);
}
sb.append("#" + t + " " + answer + "\n");
}
System.out.print(sb);
}
public static void setQueen(int[] col, int row) {
if(row == N) {
answer++;
}else {
for(int i = 1; i <= N; i++) {
col[row+1] = i;
if(isValid(col, row+1)) setQueen(col, row+1);
}
}
}
public static boolean isValid(int[] col, int row) {
for(int i=1; i < row; i++) {
if(col[i] == col[row] || (Math.abs(i - row) == Math.abs(col[i] - col[row]))) return false;
}
return true;
}
}