Hi there! So I've been tasked with a problem regarding the "Eight Queens" puzzle. The eight queens' puzzle is the problem of placing eight queens on an 8×8 chessboard so that no
two queens can attack each other immediately. The detailed description can be found at following
Wikipedia page. https://en.wikipedia.org/wiki/Eight_queens_puzzle
I have the original code for this program already as follows:
#include <iostream>
usingnamespace std;
constint NUMBER_OF_QUEENS = 8; // Constant: eight queens
int queens[NUMBER_OF_QUEENS];
// Check whether a queen can be placed at row i and column j
bool isValid(int row, int column)
{
for (int i = 1; i <= row; i++)
if (queens[row - i] == column // Check column
|| queens[row - i] == column - i // Check upper left diagonal
|| queens[row - i] == column + i) // Check upper right diagonal
returnfalse; // There is a conflict
returntrue; // No conflict
}
// Display the chessboard with eight queens
void printResult()
{
cout << "\n---------------------------------\n";
for (int row = 0; row < NUMBER_OF_QUEENS; row++)
{
for (int column = 0; column < NUMBER_OF_QUEENS; column++)
printf(column == queens[row] ? "| Q " : "| ");
cout << "|\n---------------------------------\n";
}
}
// Search to place a queen at the specified row
bool search(int row)
{
if (row == NUMBER_OF_QUEENS) // Stopping condition
returntrue; // A solution found to place 8 queens in 8 rows
for (int column = 0; column < NUMBER_OF_QUEENS; column++)
{
queens[row] = column; // Place a queen at (row, column)
if (isValid(row, column) && search(row + 1))
returntrue; // Found, thus return true to exit for loop
}
// No solution for a queen placed at any column of this row
returnfalse;
}
int main()
{
search(0); // Start search from row 0. Note row indices are 0 to 7
printResult(); // Display result
return 0;
}
However, I need to modify this program so that it can start the search from a row as inputted by the console in main().
#include <iostream>
usingnamespace std;
constint NUMBER_OF_QUEENS = 8; // Constant: eight queens
int queens[NUMBER_OF_QUEENS];
// Check whether a queen can be placed at row i and column j
bool isValid(int row, int column)
{
for (int i = 1; i <= row; i++)
if (queens[row - i] == column // Check column
|| queens[row - i] == column - i // Check upper left diagonal
|| queens[row - i] == column + i) // Check upper right diagonal
returnfalse; // There is a conflict
returntrue; // No conflict
}
// Display the chessboard with eight queens
void printResult(int row)
{
cout << "\n---------------------------------\n";
for (int tempRow = 0; tempRow < NUMBER_OF_QUEENS; tempRow++)
{
for (int column = 0; column < NUMBER_OF_QUEENS; column++)
printf(column == queens[tempRow] ? "| Q " : "| ");
cout << "|\n---------------------------------\n";
}
}
// Search to place a queen at the specified row
bool search(int row)
{
if (row == NUMBER_OF_QUEENS) // Stopping condition
returntrue; // A solution found to place 8 queens in 8 rows
for (int column = 0; column < NUMBER_OF_QUEENS; column++)
{
queens[row] = column; // Place a queen at (row, column)
if (isValid(row, column) && search(row + 1))
returntrue; // Found, thus return true to exit for loop
}
// No solution for a queen placed at any column of this row
returnfalse;
}
int main()
{
int inputRow;
cout << "Enter the row to search from:" << endl;
cin >> inputRow;
search(inputRow); // Start search from row 0. Note row indices are 0 to 7
printResult(inputRow); // Display result
return 0;
}
Any help is much appreciated. Thanks!
Oh also this is the output for an input of "2". http://ultraimg.com/image/IR7a
The program appears to be ignoring the first "i" rows for whatever the input is, in this case the first two rows.