문제 링크
https://programmers.co.kr/learn/courses/30/lessons/12952
코딩테스트 연습 - N-Queen
가로, 세로 길이가 n인 정사각형으로된 체스판이 있습니다. 체스판 위의 n개의 퀸이 서로를 공격할 수 없도록 배치하고 싶습니다. 예를 들어서 n이 4인경우 다음과 같이 퀸을 배치하면 n개의 퀸은
programmers.co.kr
풀이
백준 N-Queen 이랑 같은 문제
https://zzion2.tistory.com/293
[백준 9663번] N-Queen - python
문제 링크 https://www.acmicpc.net/problem/9663 9663번: N-Queen N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다. N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는..
zzion2.tistory.com
cnt = 0
def dfs(depth,n):
global cnt, graph
# 다 놓았으면 개수 증가
if depth == n :
cnt += 1
else :
for i in range(n):
graph[depth] = i
result = True
# 놓을 수 있는지
for j in range(depth):
if graph[depth] == graph[j] or abs(graph[depth]- graph[j]) == abs(depth-j):
result = False
break
# 놓을 수 있으면
if result :
dfs(depth+1,n)
def solution(n):
global graph, cnt
graph = [0] * n
dfs(0,n)
return cnt
'개발 > algorithm' 카테고리의 다른 글
[프로그래머스] [level3] 하노이의 탑 - python (0) | 2022.04.28 |
---|---|
[프로그래머스][level3] 110 옮기기 - python (0) | 2022.04.28 |
[백준 9663번] N-Queen - python (0) | 2022.04.28 |
[프로그래머스][level2] k진수에서 소수 개수 구하기 - python (0) | 2022.04.27 |
[프로그래머스][level2] [3차] 파일명 정렬 - python (0) | 2022.04.27 |