1
#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

TheLiv
  • 11
  • 2
  • Try using a debugger. If you don't know how, just use print statements, or learn how. It's a valuable skill. If you don't want to learn, just try pretending you're the computer and follow the instructions you gave it. – starball Sep 08 '22 at 05:29
  • 3
    Where in the code do you set any `board` element to `true`? – Daniel Langr Sep 08 '22 at 05:34
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 08 '22 at 09:01
  • @TheLiv you set the board to all false. Daniel Langr and I would like to know if you wish to set any to true? – James Risner Sep 11 '22 at 16:05

0 Answers0