Question

# -*- 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

Hello all! How can I find the unique solutions of this recursive algorithm of N-queens puzzle? It finds only all solutions: on board 8x8 it will be 92 solutions, but unique is only 12 (the other solutions are translations and mirrored from this 12)

Was it helpful?

Solution

I think these is usefull: link1 link2

For Best result you should design your algorithm by dynomic-programing and can find it in: google Stackoverflow

you set an array a[n][count] and save state i in a[..][i]. this is sample for item first by n=8 :

a = {5,1,8,4,2,7,3,6}{1} , .....

Note: each solution can changed to 8 state by symmetry and rotation.So for each result you symmetry and rotation their solution to check is that save in your array or not?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top