-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241201-9663.java
55 lines (46 loc) · 1.26 KB
/
241201-9663.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int N;
static boolean[][] board;
static int answer;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
board = new boolean[N][N];
solve(0);
System.out.print(answer);
}
static void solve(int r) {
if (r == N) {
answer++;
return;
}
for (int c = 0; c < N; c++) {
if (isPossible(r, c)) {
board[r][c] = true;
solve(r + 1);
board[r][c] = false;
}
}
}
static boolean isPossible(int r, int c) {
for (int i = 0; i < r; i++) {
if (board[i][c]) {
return false;
}
}
for (int i = r - 1, j = c - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j]) {
return false;
}
}
for (int i = r - 1, j = c + 1; i >= 0 && j < N; i--, j++) {
if (board[i][j]) {
return false;
}
}
return true;
}
}