# -*- coding: utf-8 -*-
def puzzle(rows, cols):
if rows == 0:
return [[]]
else:
return new_queen(rows - 1, cols, puzzle(rows - 1, cols))
def new_queen(new_row, cols, plsd_queens):
new_solutions = []
for solution in plsd_queens:
for new_col in range(cols):
if test(new_row, new_col, solution):
new_solutions.append(solution + [new_col])
return new_solutions
def test(new_row, new_col, solution):
for row in range(new_row):
if solution[row] == new_col or solution[row] + row == new_col + new_row or\
solution[row] - row == new_col - new_row:
return False
return True
大家好!我怎样才能找到这个 N 皇后谜题递归算法的唯一解?它只找到所有解决方案:在 8x8 上它将是 92 个解决方案,但唯一的是只有 12 个(其他解决方案是翻译并从这 12 个镜像)