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

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

merged 1 commit into from
Aug 30, 2023

Conversation

Woo-Yeol
Copy link
Member

PR Summary

N-Queen
https://www.acmicpc.net/problem/9663

풀이시간
BaaaaarkingDog의 백트래킹 강의를 보고 풀이를 시작
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의 값이 같은 경우를 의미한다.

@Woo-Yeol Woo-Yeol added the BOJ label Aug 30, 2023
@Woo-Yeol Woo-Yeol requested a review from limstonestone August 30, 2023 01:51
@Woo-Yeol Woo-Yeol self-assigned this Aug 30, 2023
@Woo-Yeol Woo-Yeol linked an issue Aug 30, 2023 that may be closed by this pull request
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으로 주고 생각했었는데 공간 복잡도의 효율을 위해서 가변적으로 변수의 값을 통해 연산되게 하는 것이 더 좋아보이네요! 리뷰 감사합니다 도현님!

# 검사 결과 불가능한 경우일 때 다시 방문한 표시 해제
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.

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



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.

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

Copy link
Contributor

@zsmalla zsmalla left a comment

Choose a reason for hiding this comment

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

새로운 풀이를 알게되서 좋았습니다. 궁금했던 부분은 리뷰 진행시 질문을 통해 답을 다 얻은 것 같아요. 고생하셨습니다!

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.

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

@Woo-Yeol Woo-Yeol merged commit aa3b123 into main Aug 30, 2023
@Woo-Yeol Woo-Yeol deleted the wooyeol-#160 branch August 30, 2023 14:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BOJ] N-Queen
4 participants