[ create a new paste ] login | about

Link: http://codepad.org/vD8lJsx0    [ raw code | fork ]

Python, pasted on Nov 5:
#!/usr/bin/env python
import timeit

cdef solve(int y, int size):
    cdef int x, x_, y_
    if y == 0:
        return [()]
    res = []
    append = res.append
    for qs in solve(y-1, size):
        for x in range(1, size+1):
            if all(x != x_ and abs(x - x_) != y - y_ for x_, y_ in qs):
                append(qs + ((x, y),))
    return res

def queens(size):
    return solve(size, size)

for x in queens(8):
    print x

def test():
    list(queens(8))

print timeit.Timer(test).timeit(100)


Create a new paste based on this one


Comments: