Skip to content

Commit 552d017

Browse files
authored
Merge pull request #354 from AlgorithmWithGod/Seol-JY
2 parents c545872 + 8d194d2 commit 552d017

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.io.*;
3+
4+
public class Main {
5+
private static int N;
6+
private static int count = 0;
7+
private static int cols = 0;
8+
private static int diag1 = 0;
9+
private static int diag2 = 0;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
N = Integer.parseInt(br.readLine());
14+
15+
solve(0);
16+
System.out.println(count);
17+
}
18+
19+
private static void solve(int row) {
20+
if (row == N) {
21+
count++;
22+
return;
23+
}
24+
25+
for (int col = 0; col < N; col++) {
26+
int colBit = 1 << col;
27+
int diag1Bit = 1 << (row + col);
28+
int diag2Bit = 1 << (row - col + N - 1);
29+
30+
if ((cols & colBit) == 0 && (diag1 & diag1Bit) == 0 && (diag2 & diag2Bit) == 0) {
31+
cols |= colBit;
32+
diag1 |= diag1Bit;
33+
diag2 |= diag2Bit;
34+
35+
solve(row + 1);
36+
37+
cols &= ~colBit;
38+
diag1 &= ~diag1Bit;
39+
diag2 &= ~diag2Bit;
40+
}
41+
}
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)