-1

我想在棋盘上放置 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

4 回答 4

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 个解决方案。

于 2013-02-03T09:40:37.100 回答
0

这是一种可能的解决方案,仅用六行 python 即可解决问题。

于 2013-02-03T09:33:55.680 回答
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
于 2013-02-20T14:31:04.430 回答
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 . . . .
于 2021-02-08T04:42:18.540 回答