Could someone please help me write this C++ program. The instructions are posted
ID: 3741971 • Letter: C
Question
Could someone please help me write this C++ program. The instructions are posted below:For this project, you will implement the recursive solution to the Eight Queens Problem from the textbook. If you need a refresher on the Eight Queens Problem, check out this video https://www.youtube.com/watch?v=jPcBU0Z2Hj8
Your solution will work for boards of size 4 through 12. The number of queens you attempt to place on the board is determined by the board size. So, a 4x4 board has 4 queens placed on it and a 12x12 board has 12 queens placed on it.
Requirements
The file Chessboard.hpp which contains the header and definitions for the class Chessboard that, at the minimum: Stores the board as a dynamic char array initially populated with ' ' space representing the board has no pieces on it. A constructor that defaults the board to 8x8. A constructor that allows the size of the board n to be given and creates the board as nxn. Has a set method that takes the row, column, and char to set in that square of the board. Use 'Q' to represent a queen. A get method that, given row and column numbers, returns the char in that square. A get method that will return the size of the board n where the board is nxn. A method that will display the contents of the board to the console. It should display Q for each queen and X for each blank (see below for an example). The recursive method placeQueens(). Follow the pseudocode on pages 179-180. The method doEightQueens() which calls the recursive method placeQueens(). Other appropriate/needed methods for your solution. The file Project2.cpp which, at a minimum: Prompts the user for the size of the board they'd like to solve the Eight Queens Problem on. Don't allow a board size of less than 4 or greater than 12. Creates a board of that size and calls doEightQueens(). Displays the solution found to the screen. (Note there should always be a solution for any board size 4 - 12.) Sample Output
A run of your program should look something like this:
Hello! I'd like to solve the Eight Queens Problem for you. How large is the board? 4 Thanks! Looks like we're solving the Four Queens Problem today!
Here is a solution I found for the placement of queens on a 4x4 board: X X Q X
Q X X X
X X X Q
X Q X X Other requirements
Follow the course code style guide and good programming practices. Make sure your code handles incorrect/unusual data properly. If you wrote the program in an environment other than Visual Studio 2017 it is your responsibility to ensure that it works in VS 2017 (which is what I’ll be grading in). Put a comment at the top of all files with your full name, class number, and assignment name like so: // Joe Smith // CS Programming Project 1 Could someone please help me write this C++ program. The instructions are posted below:
For this project, you will implement the recursive solution to the Eight Queens Problem from the textbook. If you need a refresher on the Eight Queens Problem, check out this video https://www.youtube.com/watch?v=jPcBU0Z2Hj8
Your solution will work for boards of size 4 through 12. The number of queens you attempt to place on the board is determined by the board size. So, a 4x4 board has 4 queens placed on it and a 12x12 board has 12 queens placed on it.
Requirements
The file Chessboard.hpp which contains the header and definitions for the class Chessboard that, at the minimum: Stores the board as a dynamic char array initially populated with ' ' space representing the board has no pieces on it. A constructor that defaults the board to 8x8. A constructor that allows the size of the board n to be given and creates the board as nxn. Has a set method that takes the row, column, and char to set in that square of the board. Use 'Q' to represent a queen. A get method that, given row and column numbers, returns the char in that square. A get method that will return the size of the board n where the board is nxn. A method that will display the contents of the board to the console. It should display Q for each queen and X for each blank (see below for an example). The recursive method placeQueens(). Follow the pseudocode on pages 179-180. The method doEightQueens() which calls the recursive method placeQueens(). Other appropriate/needed methods for your solution. The file Project2.cpp which, at a minimum: Prompts the user for the size of the board they'd like to solve the Eight Queens Problem on. Don't allow a board size of less than 4 or greater than 12. Creates a board of that size and calls doEightQueens(). Displays the solution found to the screen. (Note there should always be a solution for any board size 4 - 12.) Sample Output
A run of your program should look something like this:
Hello! I'd like to solve the Eight Queens Problem for you. How large is the board? 4 Thanks! Looks like we're solving the Four Queens Problem today!
Here is a solution I found for the placement of queens on a 4x4 board: X X Q X
Q X X X
X X X Q
X Q X X Other requirements
Follow the course code style guide and good programming practices. Make sure your code handles incorrect/unusual data properly. If you wrote the program in an environment other than Visual Studio 2017 it is your responsibility to ensure that it works in VS 2017 (which is what I’ll be grading in). Put a comment at the top of all files with your full name, class number, and assignment name like so: // Joe Smith // CS Programming Project 1 Could someone please help me write this C++ program. The instructions are posted below:
For this project, you will implement the recursive solution to the Eight Queens Problem from the textbook. If you need a refresher on the Eight Queens Problem, check out this video https://www.youtube.com/watch?v=jPcBU0Z2Hj8
Your solution will work for boards of size 4 through 12. The number of queens you attempt to place on the board is determined by the board size. So, a 4x4 board has 4 queens placed on it and a 12x12 board has 12 queens placed on it.
Requirements
The file Chessboard.hpp which contains the header and definitions for the class Chessboard that, at the minimum: Stores the board as a dynamic char array initially populated with ' ' space representing the board has no pieces on it. A constructor that defaults the board to 8x8. A constructor that allows the size of the board n to be given and creates the board as nxn. Has a set method that takes the row, column, and char to set in that square of the board. Use 'Q' to represent a queen. A get method that, given row and column numbers, returns the char in that square. A get method that will return the size of the board n where the board is nxn. A method that will display the contents of the board to the console. It should display Q for each queen and X for each blank (see below for an example). The recursive method placeQueens(). Follow the pseudocode on pages 179-180. The method doEightQueens() which calls the recursive method placeQueens(). Other appropriate/needed methods for your solution. The file Project2.cpp which, at a minimum: Prompts the user for the size of the board they'd like to solve the Eight Queens Problem on. Don't allow a board size of less than 4 or greater than 12. Creates a board of that size and calls doEightQueens(). Displays the solution found to the screen. (Note there should always be a solution for any board size 4 - 12.) Sample Output
A run of your program should look something like this:
Hello! I'd like to solve the Eight Queens Problem for you. How large is the board? 4 Thanks! Looks like we're solving the Four Queens Problem today!
Here is a solution I found for the placement of queens on a 4x4 board: X X Q X
Q X X X
X X X Q
X Q X X Other requirements
Follow the course code style guide and good programming practices. Make sure your code handles incorrect/unusual data properly. If you wrote the program in an environment other than Visual Studio 2017 it is your responsibility to ensure that it works in VS 2017 (which is what I’ll be grading in). Put a comment at the top of all files with your full name, class number, and assignment name like so: // Joe Smith // CS Programming Project 1
Explanation / Answer
Answer:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#define N 8
using namespace std;
void displaySolution(int value[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout<<value[i][j]<<" ";
cout<<endl;
}
}
bool isTest(int value[N][N], int value1, int value2)
{
int i, j;
for (i = 0; i < value2; i++)
{
if (value[value1][i])
return false;
}
for (i = value1, j = value2; i >= 0 && j >= 0; i--, j--)
{
if (value[i][j])
return false;
}
for (i = value1, j = value2; j >= 0 && i < N; i++, j--)
{
if (value[i][j])
return false;
}
return true;
}
bool testMethod(int value[N][N], int value2)
{
if (value2 >= N)
return true;
for (int i = 0; i < N; i++)
{
if ( isTest(value, i, value2) )
{
value[i][value2] = 1;
if (testMethod(value, value2 + 1) == true)
return true;
value[i][value2] = 0;
}
}
return false;
}
bool Method1()
{
int value[N][N] = {0};
if (testMethod(value, 0) == false)
{
cout<<"Solution does not exist"<<endl;
return false;
}
displaySolution(value);
return true;
}
int main()
{
Method1();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.