

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


queens:=proc(n)
  local a,u,v,m,aux;
  a:=[$1..n];
  u:=[true$2*n-1];
  v:=[true$2*n-1];
  m:=[];
  aux:=proc(i)
    local j,k,p,q;
    if i>n then
      m:=[op(m),copy(a)];
    else
      for j from i to n do
        k:=a[j];
        p:=i-k+n;
        q:=i+k-1;
        if u[p] and v[q] then
          u[p]:=false;
          v[q]:=false;
          a[j]:=a[i];
          a[i]:=k;
          aux(i+1);
          u[p]:=true;
          v[q]:=true;
          a[i]:=a[j];
          a[j]:=k;
        fi;
      od;
    fi;
  end;
  aux(1);
  m
end:          
 
for a in queens(8) do printf("%a\n",a) od;