注册 登录
编程论坛 Python论坛

python 八皇后问题

robin17 发布于 2019-04-12 13:14, 1369 次点击
import random
def conflict(state,nextx):
    nexty = len(state)
    for i in range(nexty):
        if abs(state[i]-nextx) in (0,nexty-i):
            return True
    return False
def queens(num=8,state=()):
    for pos in range(num):
        if not conflict(state,pos):
            if len(state) == num - 1:
                yield (pos,)---------------------------------------------------------当前面三个(1,3,0)都满足,最后一行满足条件,比如为2时,返回的是(2,)就终止循环了,请问(2,)是怎么添加到元组中,变成(1,3,0,2)的?
            else:
                for result in queens(num,state + (pos,)):
                    yield (pos,) + result
1 回复
#2
我没注册2019-04-15 09:50
增加人气,我现在还没有入门呢。只会打印hello,world。
1