-
Notifications
You must be signed in to change notification settings - Fork 2
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
24-SeongHoonC #192
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μμ μ νμλ λ¬Έμ λ° λ¦¬λ§μΈλ ν κ²Έ λ€μ νμ΄λ΄€μλλ€.
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)
μ΄ λ¬Έμ μ²μ νμμ λ λ리μ κΉκ² λ°νμ κ·Έλ°μ§ μμ§ νμ΄λ₯Ό λ°λ‘ λ μ¬λ¦΄ μ μμμ΄μ.
μ λ μ λ μ λ νλ‘μλ λͺ» νΈλ λ¬Έμ ,,, μΈ κ² κ°μ΅λλ€.
κ·Όλ° μ΄μ ν λ² μ½μ§νλ©΄ μ€λ κΈ°μ΅μ λ¨λ....
κ³ μνμ ¨μλλ₯~~~~~~~ πππ
// λκ°μ | ||
if (row - i == kotlin.math.abs(board[row] - board[i])) { | ||
return false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€νΈλ μ λκ°μΌλ‘ νλ©΄ 2μ€μ§λ¦¬λ₯Ό ν μ€λ‘ μ€μΌ μλ μκ² κΎΌμ
There was a problem hiding this 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))
μμ μ "ν " κ΅μλμ΄ μ΄λ¬Έμ λ₯Ό μ’μνμ
μ λ°±μ€μ μκΈΈλ νμ΄λ΄€λλ° βͺοΈπμ λ§λ‘λ ν΄λΉ λ‘μ§μμ 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) |
π λ¬Έμ λ§ν¬
n-queen
βοΈ μμλ μκ°
3μκ°
β¨ μλ μ½λ
2μ°¨μμΌλ‘ λͺ¨λ κ²½μ°μμ λ€ νμνλ €λ€κ°
μμ§ν μ§μ§ 무μ¨λ§μΈμ§ νλλ λͺ¨λ₯΄κ² μ΄μ νμ΄ λ΄€λλ°λ λͺ°λΌμ κ΅ν©μ μλνν λ¬Όμ΄λ΄€μ΅λλ€...
νμ μμλλ‘ κ·Έλ¦Όμ κ·Έλ €λ¨λλ° νμ΄μ¬μΌλ‘ νλ©΄ μνλ¦°λ€λ μκΈ°λ μμ΄μ μ°Έκ³ ν΄μ£ΌμΈμ..
μμμλΆν° κ° νμ νλμ© λμκ±΄λ° λ΄ μλ‘ μΈλ‘κ° κ²ΉμΉλμ§ λκ°μ μ΄ κ²ΉμΉλμ§ κ³μ°νλ ννμ λλ€.
μλμλ νΈμ λμ§ μμκΈ° λλ¬Έμ κ³ λ €νμ§ μμ΅λλ€.
n μ΄ 4λ‘ μ λ ₯λμλ€κ³ μκ°ν΄λ΄ μλ€.
μ΄λ κ² 0 ν 0 μ΄μ λμΌλ©΄
μ΄ λ κ°μ§ κ²½μ°μλ λΆκ°λ₯ν©λλ€.
μλ κ²½μ°λ κ°λ₯νλκΉ λ€μ μ¬κ·λ₯Ό μ€νν©λλ€.
κ·Έλ¬λ λͺ¨λ λΆκ°λ₯νκΈ° λλ¬Έμ return ν©λλ€
κ·ΈλΌ μκΉλ‘ λμκ°μ 1 ν μμλ 3μ΄μ΄ κ°λ₯ν©λλ€.
κ·ΈλΌ 2νμΌλ‘ κ°μ κ°λ₯ν κ³³μ νμνκ³ νΈμ λμ΅λλ€.
κ·Έλ¬λ 3νμ΄ λͺ¨λ λΆκ°λ₯ν©λλ€.
λ μ΄μ νμν κ³³μ΄ μμΌλ―λ‘... 0νμΌλ‘ λμκ°λλ€!
1νμ κ°λ₯ν κ³³μ΄ ν κ³³ λ°μ μκ΅°μ
2ν 0 μ΄μ΄ κ°λ₯νλ€μ
3ν 2μ΄μ 0ν, 1ν, 2νκ³Ό μ λΆ κ²ΉμΉμ§ μκΈ° λλ¬Έμ κ°λ₯ν©λλ€! κ·ΈλΌ row +1 ν΄μ μ¬κ·λ₯Ό μ€νν©λλ€..
row κ° n κ³Ό κ°μμ§λ©΄μ λ΅μ 1 μ¦κ°μν€κ³ return ν©λλ€...
λκ°μ΄ λ€λ₯Έ μ΄λ λ°λ³΅ν©λλ€.
μμ§ν μ΄λ° λ¬Έμ κ° λμ€λ©΄ ν μ μμ κ²μ΄λΌκ³ μκ°λμ§ μλ€μ..γ
π μλ‘κ² μκ²λ λ΄μ©