

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


byte SIZE = 8;
word count;
 
proc solve([*] int hist; int col) void:
    int i, j, n;
    n := dim(hist, 1);
    if col = n then
        count := count + 1;
        writeln();
        writeln("No. ", count);
        writeln("-----");
        for i from 0 upto n-1 do
            for j from 0 upto n-1 do
                write(if j=hist[i] then 'Q' 
                      elif (i+j)&1 /= 0 then ' ' 
                      else '.' fi)
            od;
            writeln()
        od
    else
        for i from 0 upto n-1 do 
            j := 0;
            while j<col and not (hist[j]=i or |(hist[j]-i) = col-j) do
                j := j + 1
            od;
            if j >= col then
                hist[col] := i;
                solve(hist, col+1)
            fi
        od
    fi
corp
 
proc nonrec main() void:
    [SIZE] int hist;
    count := 0;
    solve(hist, 0)
corp