# Backtracking with N-queens (queens) CS 362 - Spring 2011 - Ms. Katz

 Due 5pm, Friday, April 29th (60 extended project points) Goals - to explore writing a recursive backtracking algorithm - to write yet another program from scratch in your best programming form Overview
For this assignment, write a C++ program that prints all solutions of the N-queens problem for a given positive value of N. In the N-queens problem, the goal is to place N queens on an N-by-N chess board so that no two queens are on the same row, column, or diagonal. The picture shows a solution of the 4-queens problem. Note that there are 2 answers to the 4-queens problem, 92 for 8-queens, and 724 for 10-queens. Don't choose big numbers.

Specification
Your program absolutely MUST contain a recursive backtracking function. It must be written in C++. Your program must include several well-constructed and documented functions with parameters passed appropriately. Your program must use a vector as its primary data structure as described below.

Your program will obtain a positive integer N from the user and print all possible solutions for placing N queens on an NxN chess board. A solution will show the row and column location of each queen such that no two queens are on the same row, column, or diagonal. If there is no solution, there will be no output other than perhaps a label (ex. Solutions for 2 Queens) followed by empty space.

If the user enters a non-positive input, ask for input until a positive integer is provided or end of file is reached. Your program should be robust enough to handle the user typing a character. cin.clear( ) and cin.ignore( ) will be helpful.

Print all solutions for the value of N with one solution per line. Each line shows the row, column position of the N queens.

 As in many problems, choosing an appropriate data representation can make a problem easier. A solution will have exactly one queen in each row; therefore, you can represent the board with a single vector which contains the column location of the queen in the row given by each index. A solution will have exactly one queen in each column; therefore, the values in this vector will be unique values 0 to N-1. The picture at right matches the solution above. Use a recursive backtracking approach where you ensure that you have successfully placed queens in all rows 0 to r-1. You then attempt to place a queen in all the possible columns of row r. If you can successfully place a queen in column c (ensuring that you have queens placed appropriately in rows 0 to r), you try to place queens in the following rows.

If you find yourself trying to place a queen in row N, you have a solution in rows 0 to N-1. This is your base case.

You must be able to determine whether placing the current row's queen in a column will conflict with any other previously placed queens. There are three kinds of possible conflicts: row, column, and diagonal.

• They aren't in the same row due to the data representation.
• They aren't in the same column if the values in the vector are unique. Since you know you have checked the previous entries in the vector against each other, you have to check only that the current entry does not match any of the preceding entries.
• They aren't in the same diagonal if the difference in the rows is not equal to the difference in the columns. If you subtract the earlier row index from the current row index, you get a positive value. Compare that difference to the difference between the column values (values in the vector for the current queen and the earlier queen). Note that this second value will be either positive or negative and you must check both ways of subtracting. Determining the absolute value is more trouble than it's worth; just check both ways.

Only if you can successfully place the queen of row r in column c can you go on to trying the possibilities in row r+1. That's a precondition on your recursive function.

Example Output for 4-queens Problem

```(0,1) (1,3) (2,0) (3,2)
(0,2) (1,0) (2,3) (3,1)
```