将8位皇后放入国际象棋(不相互对待)! [我想要所有可能的结果!]

时间:2013-02-03 09:20:57

标签: python chess

我想把8张女王(或Vizier)放在棋盘上,以便他们都不能对待其他人! 喜欢这张图片

首先,
我想以程序的方式做,但似乎不可能! (它会变成百行!!(即使它有效!))

我的代码

def viz(a):
    for i in range(8):
        temp = []
        if(i == a or i == a - 1 or i == a + 1):
            continue
        temp.append(i)
    temp = set(temp)
    return temp
list1=[0]*8
for i in range(8):
    list1[i]=1
    a = viz(i)
    for j in a:
        list2[j]=1
        b = viz(j)
        for h in a.intersection(b):
            list3[h]=h
            tset = a.intersection(b)
            c = viz(h)
            for n in tset.intersection(c):
                list4[]
                .
                .
                .
        list2[j]=0
    list1[i]=0

我甚至试过面向对象的风格,它也没有用。

4 个答案:

答案 0 :(得分:3)

这是许多可能的实现之一:

#! /usr/bin/python3.2

import itertools

def queens ():
    for p in itertools.permutations (range (8) ):
        yield [x for x in enumerate (p) ]

for q in queens ():
    err = False
    for a, b in ( (a, b) for a in q for b in q if a [0] < b [0] ):
        if abs (a [0] - b [0] ) == abs (a [1] - b [1] ):
            err = True
            break
    if not err: print (q)

它产生了所有92种解决方案。

答案 1 :(得分:0)

这里只有one possible solution解决了只有六行python的问题。

答案 2 :(得分:0)

我在使用这么多模块时遇到了问题,所以这是我的答案:

res = []
ill = []

def n_ligal(r,c):
    global ill
    a = 1
    while(r+a<8):
        ill.append((r+a,c))
        ill.append((r+a,c+a))
        ill.append((r+a,c-a))
        a += 1          
def r_iligal(r,c):
    global ill
    a = 1
    while(r+a<8):
        ill.remove((r+a,c))
        ill.remove((r+a,c+a))
        ill.remove((r+a,c-a))
        a += 1

def a():
    global i,j
    j = res.pop()
    i -= 1
    r_iligal(i,j)
    j+=1
    if(j==8):
        a()

i = 0
j = 0
while(i<8):
    while (j<8):
        if((i,j) not in ill):
            res.append(j)
            n_ligal(i,j)
            if(len(res) == 8):
                print(res)
            i += 1
            j = 0
            break
            elif(j==7):
                a()
            else:
                j += 1

print('Press Q to exit')
q = input()
if(q=='q'):
    raise SystemExit

答案 3 :(得分:0)

# my solution!

SIZE=8
A=[0] * SIZE  # Array solution
s=0  # Global variable to count 'solutions', you can delete it!
t=0  # Global variable to count recursion 'tests', you can delete it!

def test(queen, col):
    global t
    t+=1
    for i in range(1,queen):
        if(A[queen-i-1]==col): return 0     # Test vertical
        if(A[queen-i-1]==col-i): return 0   # Test diagonal 1 (\)
        if(A[queen-i-1]==col+i): return 0   # Test diagonal 2 (/)
    return 1

def play(queen):
    global s
    for col in range(1,SIZE+1):
        if(test(queen,col)):     # If I can play the queen...
            A[queen-1]=col       # Add queen to the solution Array
            if(queen==SIZE):     # If the last queen was played, this is a solution
                s+=1
                print("Solution: {}, {}, {}".format(s,t,A))
            else:
                play(queen+1);   # If not last queen, play the next one
            A[queen-1]=0         # Clean the solution Array

play(1)  # Start putting first queen

# 2020-02-02
# Array solution [1, 5, 8, 6, 3, 7, 2, 4] means:
#     1 . . . . . . .
#     . . . . 5 . . .
#     . . . . . . . 8
#     . . . . . 6 . .
#     . . 3 . . . . .
#     . . . . . . 7 .
#     . 2 . . . . . .
#     . . . 4 . . . .