-2

I am a C++ noob and I have just started learning it, and one of my assignments is to print a solution to the N-Queens problem, where the board would be N*N depending on user input. My IDE keeps showing me errors I don't understand in my code, even though to me it looks good and fine.

#include <iostream>
#include <array>
#include <stdexcept>

using namespace std;

int N;

bool safe(char board[N][N], int row, int col)
{
  //checks if it's safe to place a queen
  //doesn't give me any errors
}

bool placeQueen(char board[N][N], int col)
{

    for (int i = 0; i < N; i++)
    {

        if ( safe(board, i, col) )
        // says there is no matching function to call safe

        {

        board[i][col] = 1;

        if ( placeQueen(board, col + 1) ){
        //says cannot initialize parameter of type char(*)[*]
        //with an Ivalue of type char(*)[N]
            return true;
        }

        board[i][col] = 0;
        }
    }
    return false;
}
void printAnswer(char board[N][N]){
//prints the final answer
}

int main()
{
int i, j;
try{
    cout << "Enter the number of queens: ";
    cin >> N;

    char board[N][N];
    for (int i = 0; i < N; i++){
        for (int j = 0; i < N; i++){
            board[i][j] = '.';
        }
    }

    if ( placeQueen(board, 0) == false )
    //no matching function to call placeQueen
    {
        throw runtime_error("Solution does not exist.");
        return 0;
    }

    printAnswer(board);
    //no matching function to call printAnswer
}
catch (runtime_error& excpt){
    cout << excpt.what();
}

return 0;
}

It's probably me just being stupid but help would be appreciated, thanks!

  • What's your problem? See [mcve]. – iBug Oct 05 '18 at 10:10
  • 3
    `N` needs to be a compile-time constant for this to work. Otherwise, use dynamic containers like `std::vector`. – WhozCraig Oct 05 '18 at 10:12
  • Note that your logic in `placeQueen` does not stop trying to place in columns beyond the end of the board. You should check `col` against `N`, and return `true` if they are equal – Caleth Oct 05 '18 at 10:39

1 Answers1

0

char board[N][N] is not C++ when N is not a compile time constant. It's an extension by gcc that really shouldn't be on by default.

You are not defining functions that take (C style) arrays of arrays of char, but instead they take something that isn't defined in standard C++, and behaves differently to how it would in C.

You should instead define some other type as your board, e.g. using Board = std::vector<std::vector<char>>;. Then you can pass (references to) this type around.

#include <iostream>
#include <vector>

using Board = std::vector<std::vector<char>>;

bool safe(const Board & board, int row, int col)
{
  //checks if it's safe to place a queen
  //doesn't give me any errors
}

bool placeQueen(Board & board, int col)
{    
    for (int i = 0; i < N; i++)
    {    
        if (safe(board, i, col) )
        {
            board[i][col] = 1;

            if ( placeQueen(board, col + 1) ){
                return true;
            }

            board[i][col] = 0;
        }
    }
    return false;
}

void printAnswer(const Board & board){
//prints the final answer
}

int main()
{
    std::cout << "Enter the number of queens: ";
    int N;
    std::cin >> N;

    Board board{N, {N, '.'}}; // Initialise N vectors of N '.'

    if (!placeQueen(board, 0))
    {
        std::cout << "Solution does not exist.";
        return 0;
    }

    printAnswer(board);

    return 0;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75