求助百度百科八皇后问题python代码解度
本帖最后由 qianningmogen 于 2020-6-8 10:49 编辑python代码部分:
def queen(A, cur=0):
if cur == len(A):
print(A)
return 0 #求助此处返回值何用
for col in range(len(A)):
A, flag = col, True #求助此句语法解析
for row in range(cur):
if A == col or abs(col - A) == cur - row:
flag = False
break
if flag:
queen(A, cur+1)
queen(*8)
问题一:return 0 #求助此处返回值何用
问题二:A, flag = col, True #求助此句语法解析
附百度百科八皇后问题原址:https://baike.baidu.com/item/%E5%85%AB%E7%9A%87%E5%90%8E%E9%97%AE%E9%A2%98/11053477?fromtitle=%E5%85%AB%E7%9A%87%E5%90%8E&fromid=10742426&fr=aladdin
def queen(A, cur=0):
if cur == len(A):
print(A)
return 0 #停止执行这个函数
for col in range(len(A)):
A, flag = col, True #A=col,flag=True
for row in range(cur):
if A == col or abs(col - A) == cur - row:
flag = False
break
if flag:
queen(A, cur+1)
queen(*8)
页:
[1]