Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 把8个皇后放在一盘棋里(互不相待)![我想要所有可能的结果!]_Python_Chess - Fatal编程技术网

Python 把8个皇后放在一盘棋里(互不相待)![我想要所有可能的结果!]

Python 把8个皇后放在一盘棋里(互不相待)![我想要所有可能的结果!],python,chess,Python,Chess,我想把8个皇后(或维齐尔)放在一个棋盘里,这样他们中的任何一个都不能对待其他人 喜欢这张照片吗 首先, 我想以程序化的方式来做,但似乎不可能 (它将变成一百行!!(即使它能工作!)) 我的代码 def viz(a): for i in range(8): temp = [] if(i == a or i == a - 1 or i == a + 1): continue temp.append(i) tem

我想把8个皇后(或维齐尔)放在一个棋盘里,这样他们中的任何一个都不能对待其他人
喜欢这张照片吗

首先,
我想以程序化的方式来做,但似乎不可能
(它将变成一百行!!(即使它能工作!))

我的代码

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

我甚至尝试了面向对象的风格,但它也不起作用。

以下是仅用六行python就能解决问题的方法。

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

#! /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)
#/usr/bin/python3.2
进口itertools
def queens():
对于itertools.permutations(范围(8))中的p:
在枚举(p)中,收益率[x对x]
对于皇后区的q():
错误=错误
对于a,b in((a,b)对于a in q对于b in q如果a[0]

它提供了所有92种解决方案。

我在使用这么多模块时遇到了一些问题,因此以下是我的答案:

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
res=[]
ill=[]
定义n_ligal(r,c):
全球疾病
a=1

而国际象棋游戏中的英文“维齐尔”通常被称为“皇后”。用谷歌搜索“八皇后问题”,你应该会找到一些答案。我在找到解决方法方面没有问题,实际上我以前用谷歌搜索过!但我不会写程序!!!这个问题可能更适合你
# 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 . . . .