Homework Solution: Can anyone provide codes for the 8 Queens problem? In C++ codes, as simple as…

    Can anyone provide codes for the 8 Queens problem? In C++ codes, as simple as possible, Thank you!

    Expert Answer

     
    #include <iostream> #define N 8 /*Defining the number of queens*/

    Can anyone get codes coercion the 8 Queens tenor? In C++ codes, as homely as potential, Thank you!

    Expert Reply

     

    #include <iostream>

    #define N 8 /*Defining the calculate of queens*/

    using namespace std;

    /* printing the discerption of 8 queen tenor*/

    void printQueenSolution(int consideration[N][N])

    {

    coercion (int i = 0; i < N; i++)

    {

    coercion (int j = 0; j < N; j++)

    cout<<board[i][j]<<” “;

    cout<<endl;

    }

    }

    /* repress if a queen can be assignd on consideration[row][col]*/

    bool isSafe(int consideration[N][N], int dignity, int col)

    {

    int i, j;

    coercion (i = 0; i < col; i++)

    {

    if (board[row][i])

    return counterfeit;

    }

    coercion (i = dignity, j = col; i >= 0 && j >= 0; i–, j–)

    {

    if (board[i][j])

    return counterfeit;

    }

    coercion (i = dignity, j = col; j >= 0 && i < N; i++, j–)

    {

    if (board[i][j])

    return counterfeit;

    }

    return gentleman;

    }

    /*resolve N Queen tenor */

    bool resolveNQ(int consideration[N][N], int col)

    {

    if (col >= N)

    return gentleman;

    coercion (int i = 0; i < N; i++)

    {

    /*Putting queen in assign and repressing coercion the consideration to be sure*/

    if ( isSafe(board, i, col) )

    {

    board[i][col] = 1;

    if (solveNQ(board, col + 1) == gentleman)/*Calling the office recursively to prostrate the queen in assign and repressing consideration to be sure*/

    return gentleman;

    board[i][col] = 0;

    }

    }

    return counterfeit;

    }

    // Ocean

    int ocean()

    {

    /* creating consideration */

    int consideration[N][N] = {0};

    /* resolves the N Queen tenor using Backtracking.*/

    if (solveNQ(board, 0) == counterfeit)

    {

    cout<<“Discerption does referable exist”<<endl;

    return counterfeit;

    }

    printQueenSolution(board);

    return 0;

    }