lundi 8 octobre 2018

Solving N queens problem using vectors, Boolean and recursion c++

I am trying to make a c++ program for solving the N-Queens puzzle. It should output:

How many queens to place on the board? 0 error: invalid input

How many queens to place on the board? 2 No solution found to place 2 queens on a 2 by 2 chess board

How many queens to place on the board? 4 .Q.. ...Q Q... ..Q.


My program recognizes the failed input. but how can I do the function using boolean and vectors is my question.

p.s: I really tried looking everywhere and tried to do it all week with no answer.

    #include<iostream>
    #include<stdexcept>
    #include<vector>

    using namespace std;

    bool placeQueen(int row, int column, vector<int>& board) {
      if (row == board.size()){

      }
      return false;
    }

    bool validPlace(int row, int column, vector<int>& board){
      for (int i = 0; i < row; i++) {
        if (board.at(row).at(i) == "Q")
          return false;
      }
      return false;
    }


    int main() {

      int nQueens;

      cout << "How many queens to place on the board? ";
      cin >> nQueens;
      vector<vector<bool> > board(nQueens, vector<bool> (nQueens));

      try {
       if (cin.fail()) {
        throw runtime_error("invalid input");
       }
       else if (nQueens <= 0) {
        throw runtime_error("invalid input");
       }
       else if (nQueens == 2 || nQueens == 3){
        cout << "No solution found to place " << nQueens << " queens on a ";
        cout << nQueens << " by " << nQueens << " chess board";
       }
       else if (nQueens == 1){
        cout << "Q";
       }
       else {


      }

      } catch (runtime_error &excpt) {
        cout << endl << "error: " << excpt.what();
      }

      return 0;
    }

Aucun commentaire:

Enregistrer un commentaire