Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-08-30] wooyeol #160 #176

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions BOJ/N-Queen/wooyeol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
N-Queen
https://www.acmicpc.net/problem/9663

풀이시간
BaaaaarkingDog의 백트래킹 강의를 보고 풀이를 시작
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 풀이가 바킹독님 풀이였군요!

00:05 ~ 01:20 (1시간 15분) - 풀이 참고

문제 조건
1 <= N <= 15

시간 복잡도 : O(n!)

접근법
무슨 알고리즘으로 풀이 할 수 있을까? -> 백 트래킹

- 퀸은 상하좌우와 대각선 방향 이동이 모두 가능합니다.

- 검사는 각 row를 기준으로 갯수를 확인해야한다.
- col이 같은 값은 체크
- 대각선을 검사하기 위해서는 x+y / x-y 의 값이 같은 경우는 체크
- 대각선은 왼쪽위에서 오른쪽 아래로 향하는 경우 (x,y)를 기준으로 x-y의 값이 같은 경우를 의미한다.
- 오른쪽 위에서 왼쪽 아래로 향하는 경우 (x,y)를 기준으로 x+y의 값이 같은 경우를 의미한다.
"""

import sys

global count

input = sys.stdin.readline

N = int(input())

count: int = 0

isused_col = [False] * 15 # col이 같은 경우 y가 같음
isused_diag = [False] * 30 # diag(왼쪽 아래 to 오른쪽 위) x+y가 같은 값
isused_diag2 = [False] * 30 # diag(왼쪽 위 to 오른쪽 아래) x-y+n-1가 같은 값

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N 의 범위가 주어져있긴 하지만 그래도 고정된 상수 대신 N 을 이용하는게 어떨까 싶습니다!
처음보고 약간 띠용?! 했습니당 ㅋㅋㅋㅋ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고민을 해보니 최대 15와 30개를 넘지는 않겠다는 생각에 15와 30으로 주고 생각했었는데 공간 복잡도의 효율을 위해서 가변적으로 변수의 값을 통해 연산되게 하는 것이 더 좋아보이네요! 리뷰 감사합니다 도현님!


def backtracking(row: int):
global count
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global 변수를 사용하지 않은 것 말고는 저랑 똑같은 풀이에요!! 고생하셨습니다 우열님!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전역 변수를 안쓰는 것까지 고려하기에는 아직 갈길이 많이 먼 것 같다는 생각이 들었습니다 ㅠ


if row == N:
count += 1
return

# 해당 행의 모든 열 검사
for col in range(N):
# 해당 위치에서 하나라도 부적합한 케이스(상하, 대각선)가 있다면 패스
if isused_col[col] or isused_diag[row + col] or isused_diag2[row - col + N - 1]:
continue

# 방문한 위치를 기준으로 접근이 부적합한 경우(상하, 대각선)에 방문 표시
isused_col[col] = True
isused_diag[row + col] = True
isused_diag2[row - col + N - 1] = True

# 다음 행 검사
backtracking(row + 1)

# 검사 결과 불가능한 경우일 때 다시 방문한 표시 해제
isused_col[col] = False
isused_diag[row + col] = False
isused_diag2[row - col + N - 1] = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수민님께도 코멘트 드렸지만 대각선의 방향을 반대로 생각하면 row-col 로 접근해도 되더라고요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 리뷰를 보고 어 음의 값이 나오는데 괜찮을까 했었는데 파이썬에서 음의 인덱스를 지원해주고 해시 값도 중복이 없어서 가능하다는 것을 알 수 있었습니다!



# 한 행씩 검사하는 함수
backtracking(0)

print(count)