#include <iostream>
using namespace std;
#define N 8
class queen_solver
{
private:
bool board[N][N] = {{false}};
public:
queen_solver() = default;
bool place_queens();
bool place_queen_in_row(int);
bool is_safe(int, int) const;
void print_solution() const;
};
int main()
{
queen_solver q;
if(q.place_queens())
q.print_solution();
else
cout << "Error - no solution found\n";
return 0;
}
bool queen_solver::place_queens()
{
return place_queen_in_row(0);
}
bool queen_solver::place_queen_in_row(int row)
{
// All queens have been placed successfully.
if (row >= N)
return true;
for (int col = 0; col < N; col++)
{
if (is_safe(row, col))
{
// queen at board[row][col]
if (place_queen_in_row(row + 1))
{
// All queens in rows below this one
// have been successfully placed.
return true;
}
}
}
// we were not able to place a queen anywhere in
// this row.
return false;
}
bool queen_solver::is_safe(int row, int col) const
{
int r;
int c;
for (r = 0; r < row; r++)
{
if (board[r][col])
{
return false;
}
}
for (r = row, c = col; r >= 0 && c >= 0; r--, c--)
{
if (board[r][c])
{
return false;
}
}
for (r = row, c = col; r >= 0 && c < N; c++, r--)
{
if (board[r][c])
{
return false;
}
}
return true;
}
void queen_solver::print_solution() const
{
int r;
int c;
for (r = 0; r < N; r++)
{
for (c = 0; c < N; c++)
{
cout <<board[r][c] << " ";
}
cout << endl;
}
}
Just wondering what I'm doing wrong here and why I'm only getting an 8 by 8 array with all 0s and no 1s to represent the queens. I have been trying to fix it for hours and I feel like it is just a dumb little mistake. If you could let me know what I did wrong that would be really appreciated Thank you! OutPut