Question: 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

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. 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.

Specifications

Your program must solve the N-queens problem using a recursive function. You need to implement a ChessPuzzle class with the member data and member functions specified in the following header file (ChessPuzzle.h).

#ifndef CHESSPUZZLE_H

#define CHESSPUZZLE_H

#include

#include

using namespace std;

class ChessPuzzle

{

public:

ChessPuzzle(int size = 8); //initializes all entries to 0

void printBoard() const;

bool canPlaceQueen(int row, int col);

bool inBoard(int row, int col);

void eightQueens(int row, int & solutions);

private:

vector< vector > board;

int N;

};

#endif

Implement all member functions in a separate source code file (e.g., ChessPuzzle.cpp). Test your recursive function using the following test file (ChessPuzzleMain.cpp).

#include "ChessPuzzle.h"

int main()

{

int size;

cout << "Enter the size of the board: ";

cin >> size;

ChessPuzzle myPuzzle(size);

int numberOfSolutions = 0;

myPuzzle.eightQueens(0, numberOfSolutions);

system("pause");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!