Skip to content

Instantly share code, notes, and snippets.

@EwhaWhiteMoon
Created January 11, 2023 07:05
Show Gist options
  • Save EwhaWhiteMoon/c8ca5f001bc68c8b257a253d8acc6785 to your computer and use it in GitHub Desktop.
Save EwhaWhiteMoon/c8ca5f001bc68c8b257a253d8acc6785 to your computer and use it in GitHub Desktop.
def solve(N : int) -> int:
ans : int = 0
arr : list[int] = [0 for i in range(0, N)]
def n_queen(depth : int):
nonlocal ans, arr
for i in range(0, N):
arr[depth] = i
if check_vaild(depth, i):
if depth < N - 1:
n_queen(depth + 1)
else:
ans += 1
def check_vaild(depth : int, cur : int) -> bool:
nonlocal arr
if depth == 0:
return True
for i in range(0, depth):
if arr[i] == cur:
return False
if (arr[i] + depth - i) == cur:
return False
if (arr[i] - depth + i) == cur:
return False
return True
n_queen(0)
return ans
N : int = int(input())
print(solve(N))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment