

Solve the eight queens puzzle.



You can extend the problem to solve the puzzle with a board of size   NxN.

For the number of solutions for small values of   N,   see   OEIS: A000170.



Related tasks

 A* search algorithm
 Solve a Hidato puzzle
 Solve a Holy Knight's tour
 Knight's tour
 Peaceful chess queen armies
 Solve a Hopido puzzle
 Solve a Numbrix puzzle
 Solve the no connection puzzle


n=8;
solutions=[[]];
v = 1:n;
P = perms(v);
for i=1:length(P)
    for j=1:n
        sub(j)=P(i,j)-j;
        add(j)=P(i,j)+j;
    end
    if n==length(unique(sub)) && n==length(unique(add))
        solutions(end+1,:)=P(i,:);
    end
end
 
fprintf('Number of solutions with %i queens: %i', n, length(solutions));
 
if ~isempty(solutions)
    %Print first possible solution
    board=solutions(1,:);
    s = repmat('-',n);
    for k=1:length(board)
        s(k,board(k)) = 'Q';
    end
    s
end