Place n queens on an n × n board so that no two attack each other (same row, column or diagonal).
Write NQueens. Place one queen per row with a recursive method place(row): try every column, keep it only if it is safe with respect to the rows above, recurse to the next row, and backtrack when no column works. Read n and print the number of solutions, how many queens were placed during the whole search (count one each time a queen is put on a safe square), and the first solution found (columns tried from left to right).
For the input
6
the program prints
Solutions for n = 6: 4
Queens placed during the search: 152
First solution:
.Q....
...Q..
.....Q
Q.....
..Q...
....Q.