From the course: Code Clinic: C++

Solution overview: Eight queens - C++ Tutorial

From the course: Code Clinic: C++

Start my 1-month free trial

Solution overview: Eight queens

- [Instructor] The solution I created for the Eight Queens Challenge can be applied to any sized board, such as eight on a standard eight by eight board, or nine queens on a nine by nine board, or five queens on a five by five board. As long as the board is four by four or larger, the same algorithm can be applied to find the solutions. So instead of writing a program that just solves the problem for eight queens, I wrote one that will solve N queens on an N by N board where N is greater than four. My N Queens solution can be found in the Exercise Files, chapter three, solution folder. I've already compiled the single source file, NQueens.cpp using GCC through Cygwin. To run the executable, I'll open a command prompt by typing cmd into the file explorer address bar. In the command prompt, I'll type NQueens.exe followed by the size of the board a want to solve for. Let's try eight. By default, my executable returns one possible solution to the problem, displaying where the queens would be placed on an eight by eight chessboard. Below the board, I also display how many total possible solutions there are for an eight by eight board, which is 92. To see all the possible solutions, I'll run the program again, but this time, I'll add an additional argument of the number one. This tells my executable to print out all 92 solutions to the eight queens problem. When I hit enter, you can see all the solutions scrolling down in the command prompt. I can also find the solution for a different number of queens. I'll rerun my executable for four queens this time and display all the possible solutions. As you can see, there are much fewer possible solutions with a smaller board.

Contents