Eight Queens 2d array problem

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:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
const int 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
 return false; // There is a conflict
 return true; // 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
 return true; // 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))
 return true; // Found, thus return true to exit for loop
 }
 // No solution for a queen placed at any column of this row
 return false;
}
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().

This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
const int 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
			return false; // There is a conflict
	return true; // 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
		return true; // 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))
			return true; // Found, thus return true to exit for loop
	}
	// No solution for a queen placed at any column of this row
	return false;
}
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.
Last edited on
Topic archived. No new replies allowed.