-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Gold IV] Title: N-Queen, Time: 29532 ms, Memory: 207912 KB -BaekjoonHub
- Loading branch information
1 parent
cc154b8
commit 3daabd3
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# [참고 자료](https://hstory0208.tistory.com/entry/Python%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EB%B0%B1%EC%A4%80-9663%EB%B2%88-N-Queen) | ||
# TODO: 베껴서 다시 풀어야함 | ||
|
||
INIT_VALUE = 0 | ||
N = int(input()) | ||
# map = [[False] * N for _ in range(N)] # 주의) 같은 참조로 여러개 만들면 안됨! | ||
# (최적화) 2D > 1D | ||
# - y_pos_arr[x_pos] = y_pos; map[x_pos][y_pos] 에 퀸을 놓겠다. | ||
# - 한편으로는 모두 다른 숫자를 갖고 있어야겠네 | ||
y_pos_arr = [INIT_VALUE] * N | ||
|
||
|
||
def checkValidPos(target_x_pos): | ||
|
||
# 대각선 탐색 | ||
for i in range(target_x_pos): | ||
is_in_same_line = y_pos_arr[target_x_pos] == y_pos_arr[i] | ||
is_in_same_cross = abs(y_pos_arr[target_x_pos] - y_pos_arr[i]) == abs( | ||
target_x_pos - i | ||
) | ||
|
||
if is_in_same_line or is_in_same_cross: | ||
return False | ||
|
||
return True | ||
|
||
|
||
cnt = 0 | ||
|
||
|
||
def travel(x_pos): # (0 >> N) 현재 x_pos 고정해서 위치 찾는 중 | ||
global cnt | ||
if x_pos == N: | ||
cnt += 1 # 탈출 - 끝까지 도달 성공 | ||
return | ||
|
||
for y_pos in range(N): | ||
y_pos_arr[x_pos] = y_pos | ||
valid = checkValidPos(x_pos) | ||
if valid: | ||
travel(x_pos + 1) | ||
|
||
|
||
travel(0) | ||
print(cnt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# [Gold IV] N-Queen - 9663 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/9663) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 207912 KB, 시간: 29532 ms | ||
|
||
### 분류 | ||
|
||
백트래킹, 브루트포스 알고리즘 | ||
|
||
### 제출 일자 | ||
|
||
2024년 8월 12일 02:45:36 | ||
|
||
### 문제 설명 | ||
|
||
<p>N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다.</p> | ||
|
||
<p>N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오.</p> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 N이 주어진다. (1 ≤ N < 15)</p> | ||
|
||
### 출력 | ||
|
||
<p>첫째 줄에 퀸 N개를 서로 공격할 수 없게 놓는 경우의 수를 출력한다.</p> | ||
|