https://www.acmicpc.net/problem/9663
def init():
n = int(input())
count = 0
cols = [False] * n
up = [False] * (2 * n - 1)
down = [False] * (2 * n - 1)
return n, count, cols, up, down
def dfs(level):
global count
if level == n:
count += 1
return
for j in range(n):
if possible(level, j):
cols[j] = up[level + j] = down[level - j + n - 1] = True
dfs(level + 1)
cols[j] = up[level + j] = down[level - j + n - 1] = False
def possible(level, j):
if cols[j] or up[level + j] or down[level - j + n - 1]:
return False
return True
n, count, cols, up, down = init()
dfs(0)
print(count)