-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon9663.java
64 lines (48 loc) · 1.35 KB
/
Baekjoon9663.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
56
57
58
59
60
61
62
63
64
package algo.Algorithms;
import java.util.Arrays;
import java.util.Scanner;
/**
* https://www.acmicpc.net/problem/9663
* 백준 9663 n-queen
*/
public class Baekjoon9663 {
public static int[][] map;
public static int count = 0;
public static int N;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
map = new int[N][N];
dfs(0);
System.out.println(count);
}
private static void dfs(int row) {
if (row == N) {
count++;
return;
}
for (int i = 0; i < N; i++) {
if (!isAvailable(row, i)) continue;
map[row][i] = 1;
dfs(row+1);
map[row][i] = 0;
}
}
private static boolean isAvailable(int row, int col) {
//세로가 겹침
for (int i = 0; i < N; i++) {
if (i == row) continue;
if (map[i][col] == 1) return false;
}
//대각선 겹침
for (int i = 1; i < N; i++) {
if (row - i < 0 || col - i < 0) continue;
if (map[row - i][col - i] == 1) return false;
}
for (int i = 1; i < N; i++) {
if (row - i < 0 || col + i >= N) continue;
if (map[row - i][col + i] == 1) return false;
}
return true;
}
}