Skip to content

Instantly share code, notes, and snippets.

@3rogue
Last active August 29, 2015 14:25

Revisions

  1. 3rogue revised this gist Jul 26, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion eightqueen.py
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,5 @@ def OK(s):

    solutions = [c for c in cols if OK(c)]

    for i,s in enumerate(solutions,1): print i,":",s
    for i,s in enumerate(solutions,1):
    print i,":",s
  2. 3rogue created this gist Jul 26, 2015.
    13 changes: 13 additions & 0 deletions eightqueen.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    from itertools import permutations as perm
    # (1, 5, 8, 6, 3, 7, 2, 4)表示棋子位置:i.imgbox.com/VHrKYxDG.png
    cols = perm(range(1,9)) #所有可能a88=40320

    # 保证对角线
    def OK(s):
    sl = set(r-c for r,c in enumerate(s)) # y=x
    bs = set(r+c for r,c in enumerate(s)) # y=-x
    return len(sl) == len(bs) == 8

    solutions = [c for c in cols if OK(c)]

    for i,s in enumerate(solutions,1): print i,":",s