[Gold IV] Title: N-Queen, Time: 29720 ms, Memory: 32412 KB -BaekjoonHub
This commit is contained in:
37
백준/Gold/9663. N-Queen/N-Queen.py
Normal file
37
백준/Gold/9663. N-Queen/N-Queen.py
Normal 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)
|
||||
30
백준/Gold/9663. N-Queen/README.md
Normal file
30
백준/Gold/9663. N-Queen/README.md
Normal 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>
|
||||
|
||||
Reference in New Issue
Block a user