package step_by_step.level15;
import java.util.*;
public class N_Queen {
static int[] arr;
static int count = 0;
static boolean check (int row) {
for (int j = 0; j < row; j++) {
if ((arr[j]) == arr[row] || ( row - j == Math.abs(arr[row] - arr[j]))) {
return false;
}
}
return true;
}
static void nQueen(int n, int row) {
if (row == n) {
count++;
return;
}
for (int column = 0; column < n; column++) {
arr[row] = column; // 퀸의 위치 : (row, column)
if (check(row)) {
nQueen(n, row + 1);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
arr = new int[n];
nQueen(n, 0);
System.out.println(count);
}
}
- (0, 0) 부터 퀸을 한 행에 하나씩 놓을 수 있는지 확인 후 놓기
728x90
반응형
'매일코테 > 못 푼 문제' 카테고리의 다른 글
조합 0의 개수 - 시간초과 (0) | 2022.08.14 |
---|---|
백준 14단계 5번 (0) | 2022.08.12 |
참외밭 (2) | 2022.08.06 |
백준 12단계 no.4 (0) | 2022.08.05 |
백준 10단계 no.4 (0) | 2022.07.26 |