1+ package backjoon ;
2+ // https://www.acmicpc.net/problem/9663
3+ // N-Queen
4+ import java .io .BufferedReader ;
5+ import java .io .IOException ;
6+ import java .io .InputStreamReader ;
7+ 8+ public class _9663 {
9+ 10+ public static int [] arr ;
11+ public static int N ;
12+ public static int count = 0 ;
13+ 14+ public static void main (String [] args ) throws IOException {
15+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
16+ N = Integer .parseInt (br .readLine ());
17+ arr = new int [N ];
18+ 19+ nQueen (0 );
20+ System .out .println (count );
21+ }
22+ 23+ public static void nQueen (int depth ) {
24+ // λͺ¨λ μμλ₯Ό λ€ μ±μ΄ μνλ©΄ count μ¦κ° λ° return
25+ if (depth == N ) {
26+ count ++;
27+ return ;
28+ }
29+ 30+ for (int i = 0 ; i < N ; i ++) {
31+ arr [depth ] = i ;
32+ // λμ μ μλ μμΉμΌ κ²½μ° μ¬κ·νΈμΆ
33+ if (Possibility (depth )) {
34+ nQueen (depth + 1 );
35+ }
36+ }
37+ 38+ }
39+ 40+ public static boolean Possibility (int col ) {
41+ 42+ for (int i = 0 ; i < col ; i ++) {
43+ // κ°μ νμ μ‘΄μ¬ν κ²½μ°
44+ if (arr [col ] == arr [i ]) {
45+ return false ;
46+ }
47+ // λκ°μ μμ λμ¬μλ κ²½μ°
48+ else if (Math .abs (col - i ) == Math .abs (arr [col ] - arr [i ])) {
49+ return false ;
50+ }
51+ }
52+ 53+ return true ;
54+ }
55+ }
56+ /*
57+ input
58+ 8
59+
60+ output
61+ 92
62+ */
0 commit comments