Skip to content

Commit 572e316

Browse files
committed
[Gold IV] Title: N-Queen, Time: 2740 ms, Memory: 17820 KB -BaekjoonHub
1 parent 6e79889 commit 572e316

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
static int count;
5+
static boolean[] usedCol;
6+
static boolean[] usedDialg1;
7+
static boolean[] usedDialg2;
8+
9+
10+
public static void main(String[] args) {
11+
12+
Scanner scanner = new Scanner(System.in);
13+
int N = scanner.nextInt();
14+
15+
usedCol = new boolean[N];
16+
usedDialg1 = new boolean[2*N];
17+
usedDialg2 = new boolean[2*N];
18+
int row = 0;
19+
count = 0;
20+
dfs(row,N);
21+
System.out.println(count);
22+
}
23+
public static void dfs(int row,int N){
24+
25+
//행당 하나씩 퀸을 두면서 내려간다.
26+
27+
if (row == N){
28+
count += 1;
29+
return;
30+
}
31+
for (int col = 0; col < N; col++) {
32+
if (usedCol[col] == true || usedDialg1[row - col + N] == true || usedDialg2[row + col] == true){
33+
continue;
34+
}
35+
usedCol[col] = true; // 한 열
36+
usedDialg1[row - col + N] = true; // 우하향 대각선
37+
usedDialg2[row + col] = true; // 좌화향 대각선
38+
dfs(row+1,N);
39+
40+
//백 트래킹 마킹 해제
41+
42+
usedCol[col] = false;
43+
usedDialg1[row - col + N] = false;
44+
usedDialg2[row + col] = false;
45+
}
46+
47+
}
48+
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Gold IV] N-Queen - 9663
2+
3+
[문제 링크](https://www.acmicpc.net/problem/9663)
4+
5+
### 성능 요약
6+
7+
메모리: 17820 KB, 시간: 2740 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2025년 6월 25일 16:15:18
16+
17+
### 문제 설명
18+
19+
<p>N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다.</p>
20+
21+
<p>N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오.</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 N이 주어진다. (1 ≤ N < 15)</p>
26+
27+
### 출력
28+
29+
<p>첫째 줄에 퀸 N개를 서로 공격할 수 없게 놓는 경우의 수를 출력한다.</p>
30+

0 commit comments

Comments
 (0)