Skip to content

Instantly share code, notes, and snippets.

@youminkim
Created August 18, 2014 06:59
Show Gist options
  • Save youminkim/554465b9b621305a350d to your computer and use it in GitHub Desktop.
Save youminkim/554465b9b621305a350d to your computer and use it in GitHub Desktop.
nqueen.py
import sys
rl = lambda : raw_input()
problems = int(rl())
cnt = 0
B = []
N = 99
def p(n):
ret = range(N)
for b in B:
i,j = b[0], b[1]
for k in [j, j+(n-i), j-(n-i)]:
if k in ret and k < N and k >=0:
ret.remove(k)
return [(n,a) for a in ret]
def solve(n):
global cnt, B, N
if n == N:
cnt+=1
return
possibles = p(n)
for c in possibles:
B.append(c)
solve(n+1)
B.remove(c)
for a in range(problems):
n = int(rl())
cnt = 0
B = []
N = n
solve(0)
print cnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment