File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments