Created
August 18, 2014 06:59
-
-
Save youminkim/554465b9b621305a350d to your computer and use it in GitHub Desktop.
nqueen.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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