File tree 1 file changed +62
-0
lines changed
1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
You canโt perform that action at this time.
0 commit comments