[Gold IV] Title: N-Queen, Time: 29720 ms, Memory: 32412 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-11 14:03:26 +09:00
parent 22410b097f
commit deff6a3e26
2 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import sys
input = sys.stdin.readline
n = int(input().strip())
colUsed = [False] * (n)
diagUsed = [False] * ((2*n)-1)
AntidiagUsed = [False] * ((2*n)-1)
tot = 0
def placeQueens(row):
global tot
if (row == n):
tot+=1
return
for col in range(n):
diagIndex = row - col + n - 1
AntiDiagIndex = row + col
if not colUsed[col] and not diagUsed[diagIndex] and not AntidiagUsed[AntiDiagIndex]:
colUsed[col] = True
diagUsed[diagIndex] = True
AntidiagUsed[AntiDiagIndex] = True
placeQueens(row + 1)
colUsed[col] = False
diagUsed[diagIndex] = False
AntidiagUsed[AntiDiagIndex] = False
placeQueens(0)
print(tot)

View File

@@ -0,0 +1,30 @@
# [Gold IV] N-Queen - 9663
[문제 링크](https://www.acmicpc.net/problem/9663)
### 성능 요약
메모리: 32412 KB, 시간: 29720 ms
### 분류
백트래킹, 브루트포스 알고리즘
### 제출 일자
2025년 3월 11일 14:02:42
### 문제 설명
<p>N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다.</p>
<p>N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오.</p>
### 입력
<p>첫째 줄에 N이 주어진다. (1 ≤ N < 15)</p>
### 출력
<p>첫째 줄에 퀸 N개를 서로 공격할 수 없게 놓는 경우의 수를 출력한다.</p>