// initialisation, N = board size and #queens
for i=1 to N
{
u_hor[i] = 0; // clear horizontal rows
u_up[i] = 0; u_up[i+N] = 0; // clear upwards diagonals
u_down[i] = 0; u_down[i+N] = 0; // clear downwards diagonals
}
col = 1;
row = 0;
solutions = 0;
//=== main program =====
repeat
{
Succes = False;
repeat
{ // find the next row for this queen (=col)
row = row + 1;
If (row <= N) Then
Succes = Not (u_hor[row] Or u_up[row + col] Or u_down[row - col + N]);
}
Until Succes Or (row > N);
If Succes Then
{ // Good place has been found for this queen - now place it ...
If col <= N Then
{
u_hor[row] = True; // occupy this row & diagonals
u_up[row + col] = True;
u_down[row - col + N] = True;
queen[col] = row; // store her position
col = col + 1; // go to the next column with a new queen
row = 0;
//CountMoveAndDraw(); print this position - if you like
}
If col > N Then
{ // all queens are placed - we have a solution!!!
solutions = solutions + 1;
//DrawSolution(); print this solution - if you like
Succes = False; // continue by rejecting this queen
}
}
If Not Succes Then
{ // this queen is ready, has tried all rows - go back to the previous one
col = col - 1;
If col > 0 Then
{
row = queen[col];
u_hor[row] = False; // clear the row and diagonals of this queen
u_up[row + col] = False;
u_down[row - col + N] = False;
}
}
}
Until (col < 1) ;
writeln(solutions + " solutions");
//
// Note: Not written in any specific programming language, mixture between Pascal and Java,
// to make it readable for most people