python 八皇后问题
import randomdef 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