Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24-SeongHoonC #192

Merged
merged 3 commits into from
May 23, 2024
Merged

24-SeongHoonC #192

merged 3 commits into from
May 23, 2024

Conversation

SeongHoonC
Copy link
Collaborator

@SeongHoonC SeongHoonC commented May 18, 2024

πŸ”— 문제 링크

n-queen

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

3μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

2μ°¨μ›μœΌλ‘œ λͺ¨λ“  경우의수 λ‹€ νƒμƒ‰ν•˜λ €λ‹€κ°€
μ†”μ§νžˆ μ§„μ§œ λ¬΄μŠ¨λ§μΈμ§€ ν•˜λ‚˜λ„ λͺ¨λ₯΄κ² μ–΄μ„œ 풀이 λ΄€λŠ”λ°λ„ λͺ°λΌμ„œ κ΅ν™©μ„ μƒλ‹˜ν•œν…Œ λ¬Όμ–΄λ΄€μŠ΅λ‹ˆλ‹€...
탐색 μˆœμ„œλŒ€λ‘œ 그림을 κ·Έλ €λ†¨λŠ”λ° 파이썬으둜 ν’€λ©΄ μ•ˆν’€λ¦°λ‹€λŠ” μ–˜κΈ°λ„ μžˆμ–΄μ„œ μ°Έκ³ ν•΄μ£Όμ„Έμš”..

μœ„μ—μ„œλΆ€ν„° 각 행에 ν•˜λ‚˜μ”© 놓을건데 λ‚΄ μœ„λ‘œ μ„Έλ‘œκ°€ κ²ΉμΉ˜λŠ”μ§€ λŒ€κ°μ„ μ΄ κ²ΉμΉ˜λŠ”μ§€ κ³„μ‚°ν•˜λŠ” ν˜•νƒœμž…λ‹ˆλ‹€.
μ•„λž˜μ—λŠ” 퀸을 놓지 μ•Šμ•˜κΈ° λ•Œλ¬Έμ— κ³ λ €ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

n 이 4둜 μž…λ ₯λ˜μ—ˆλ‹€κ³  μƒκ°ν•΄λ΄…μ‹œλ‹€.

μ΄λ ‡κ²Œ 0 ν–‰ 0 열에 λ†“μœΌλ©΄

image

이 두 가지 κ²½μš°μ—λŠ” λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.

image image

μ•„λž˜ κ²½μš°λŠ” κ°€λŠ₯ν•˜λ‹ˆκΉ λ‹€μŒ μž¬κ·€λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

image

κ·ΈλŸ¬λ‚˜ λͺ¨λ‘ λΆˆκ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— return ν•©λ‹ˆλ‹€
image

그럼 μ•„κΉŒλ‘œ λŒμ•„κ°€μ„œ 1 ν–‰ μ—μ„œλ„ 3열이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

image

그럼 2ν–‰μœΌλ‘œ κ°€μ„œ κ°€λŠ₯ν•œ 곳을 νƒμƒ‰ν•˜κ³  퀸을 λ†“μŠ΅λ‹ˆλ‹€.

image

κ·ΈλŸ¬λ‚˜ 3행이 λͺ¨λ‘ λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.

image

더 이상 탐색할 곳이 μ—†μœΌλ―€λ‘œ... 0ν–‰μœΌλ‘œ λŒμ•„κ°‘λ‹ˆλ‹€!

image

1행에 κ°€λŠ₯ν•œ 곳이 ν•œ κ³³ 밖에 μ—†κ΅°μš”

image

2ν–‰ 0 열이 κ°€λŠ₯ν•˜λ„€μš”

image

3ν–‰ 2열은 0ν–‰, 1ν–‰, 2ν–‰κ³Ό μ „λΆ€ κ²ΉμΉ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— κ°€λŠ₯ν•©λ‹ˆλ‹€! 그럼 row +1 ν•΄μ„œ μž¬κ·€λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€..

row κ°€ n κ³Ό κ°™μ•„μ§€λ©΄μ„œ 닡을 1 μ¦κ°€μ‹œν‚€κ³  return ν•©λ‹ˆλ‹€...

λ˜‘κ°™μ΄ λ‹€λ₯Έ 열도 λ°˜λ³΅ν•©λ‹ˆλ‹€.

μ†”μ§νžˆ 이런 λ¬Έμ œκ°€ λ‚˜μ˜€λ©΄ ν’€ 수 μžˆμ„ 것이라고 μƒκ°λ˜μ§€ μ•Šλ„€μš”..γ…Ž

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

μ˜ˆμ „μ— ν’€μ—ˆλ˜ 문젠데 λ¦¬λ§ˆμΈλ“œ ν•  κ²Έ λ‹€μ‹œ ν’€μ–΄λ΄€μ”λ‹ˆλ‹€.

image

import sys

def input(): return sys.stdin.readline().rstrip()

N = int(input())

answer = 0
def dfs(board, depth, N):
    global answer
    
    if depth == N:
        answer += 1
        return
    
    for col in range(N):
        for idx, prev_col in enumerate(board):
            if col == prev_col: break
            if col - (depth - idx) >= 0 and (col - (depth - idx)) == prev_col: break
            if col + (depth - idx) < N and (col + (depth - idx)) == prev_col: break
        
        else:
            board.append(col)
            dfs(board, depth+1, N)
            board.pop()
    return

dfs([], 0, N)
print(answer)

이 문제 처음 ν’€μ—ˆμ„ λ•Œ λ‡Œλ¦¬μ— 깊게 λ°•ν˜€μ„œ κ·ΈλŸ°μ§€ 아직 풀이λ₯Ό λ°”λ‘œ λ– μ˜¬λ¦΄ 수 μžˆμ—ˆμ–΄μš”.

μ ˆλŒ€ μ ˆλŒ€ μ ˆλŒ€ ν™€λ‘œμ„œλŠ” λͺ» ν‘ΈλŠ” 문제,,, 인 것 κ°™μŠ΅λ‹ˆλ‹€.




근데 이제 ν•œ 번 μ‚½μ§ˆν•˜λ©΄ 였래 기얡에 λ‚¨λŠ”....

κ³ μƒν•˜μ…¨μ”λ‹ˆλ‘₯~~~~~~~ πŸ‘πŸ‘πŸ‘

Comment on lines +35 to +38
// λŒ€κ°μ„ 
if (row - i == kotlin.math.abs(board[row] - board[i])) {
return false
}
Copy link
Member

Choose a reason for hiding this comment

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

μ˜€ν˜Έλž‘ μ ˆλŒ€κ°’μœΌλ‘œ ν•˜λ©΄ 2μ€„μ§œλ¦¬λ₯Ό ν•œ μ€„λ‘œ 쀄일 μˆ˜λ„ μžˆκ² κΎΌμš”

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

자료ꡬ쑰 과제 ν•  λ•Œ c둜 N=15일 λ•ŒκΉŒμ§€ 좜λ ₯ν•˜λŠ”κ²Œ λ¬Έμ œμ˜€λŠ”λ°
어어엉ㅁ청 였래걸렸던게 기얡에 λ‚¨λ„€μš” ...

??? : λ„ˆλ„€ μ»΄ν“¨ν„°λŠ” μ„±λŠ₯이 μ•ˆμ’‹μ•„μ„œ 좜λ ₯λ˜λŠ”λ°κΉŒμ§€ κΈΈλ©΄ λͺ‡μ‹œκ°„ κ±Έλ €

기얡을 λ”λ“¬μœΌλ©° ν’€μ–΄λ³΄μ•˜μˆ©λ‹ˆλ‹€... λ‹Ήμ‹œ μΌμ£ΌμΌλ™μ•ˆ 이 문제만 작고 μžˆμ—ˆλ˜κ²Œ 기얡이 λ‚˜λ„€μš”..γ…Žγ…Ž;;

λ¬Έμ œν‘ΈλŠλΌ κ³ μƒλ§ŽμœΌμ…¨μŠ΄λ‹€ ... πŸ‘πŸ’ͺ

import sys

MAX_N = 15
cols = [0] * MAX_N

# 퀸을 놓을 수 μžˆλŠ”κ°€ νŒλ‹¨ν•˜λŠ” ν•¨μˆ˜
def promising(level):
    for i in range(level):
        if cols[i] == cols[level] or abs(level - i) == abs(cols[level] - cols[i]):
            return False
    return True

# κ°€λŠ₯ν•œ ν€Έμ˜ 개수λ₯Ό μ„ΈλŠ” ν•¨μˆ˜
def queens(level, N):
    if level == N: # λκΉŒμ§€ κ°„ 경우
        return 1

    count = 0
    for i in range(N):
        cols[level] = i
        if promising(level):
            count += queens(level + 1, N)

    return count

def input(): return sys.stdin.readline().rstrip()

n = int(input())
print(queens(0, n))

@alstjr7437
Copy link
Member

μ˜ˆμ „μ— "흠" κ΅μˆ˜λ‹˜μ΄ 이문제λ₯Ό μ’‹μ•„ν•˜μ…”μ„œ 백쀀에 있길래 ν’€μ–΄λ΄€λŠ”λ°
"흠" κ΅μˆ˜λ‹˜μ˜ λ‘œμ§λŒ€λ‘œ μ§œλ‹ˆκΉŒ μ‹œκ°„μ΄ˆκ³Όκ°€ κ³„μ†λ‚˜μ„œ λ²„λ €λ’€λ˜ κΈ°μ–΅κ³Ό 기둝이 μžˆλ„€μš”!!

β›ͺοΈπŸ‘‘μ˜ λ§λ‘œλŠ” ν•΄λ‹Ή λ‘œμ§μ—μ„œ promising와 queens의 ν•¨μˆ˜ 호좜 회수λ₯Ό 쀄이면 λœλ‹€λŠ”λ°
아직 μ–΄λ–»κ²Œ 할지 이해가 μ•ˆλ˜μ„œ λ‹€μŒμ— ν•œλ²ˆ ν’€μ–΄λ΄μ•Όκ² λ„€μš”!!
λ¬Έμ œλŠ” ν’€λ•Œ μ—„μ²­ μž¬λ°Œμ—ˆλ˜ 것 κ°™μ•„μš”!

n = int(input())
board = [0] * (n+1)
count = 0

def promising(level):
    for i in range(1,level):
        if board[i] == board[level] or level-i == abs(board[level]-board[i]):
            return False
    return True

def queens(level):
    # queen을 λ‘˜ 수 μžˆλŠ” 곳인지 νŒλ‹¨
    if not promising(level):
        return 
    # queen이 λͺ¨λ‘ κ°€λŠ₯ν• λ•Œ base case
    if level == n:
        global count
        count += 1
        return 
    # 각 배열에 nλ²ˆμ§Έμ— μ„Έλ‘œ i둜 μžˆλŠ” μœ„μΉ˜ μΆ”κ°€
    for i in range(1,n+1):
        board[level+1] = i
        queens(level+1)

queens(0)
print(count)

@SeongHoonC SeongHoonC merged commit 93acc1f into main May 23, 2024
2 checks passed
@SeongHoonC SeongHoonC deleted the 24-SeongHoonC branch May 23, 2024 02:57
@tgyuuAn tgyuuAn mentioned this pull request May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants