Question
please explain/ comment
3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is attacking anot
3.3. Hints You might want to represent the chessboard using a 2-D array of integers or Boolean variables, e.g., int board [8]
3.3.2. Iterative Algorithm Start by placing a queen in the leftmost column of the top row. Since there are no rows above the
0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include<bits/stdc++.h> //function to check if a queen is placed at left side of (x,y) bool check(int board[8][8], int x, int

Output: 1 еееееее eeeeee1e еееееее eeeeeee1 01 ееееее eee1oooo eeeee1ee ee1eeeee

Code:

#include<bits/stdc++.h>
//function to check if a queen is placed at left side of (x,y)
bool check(int board[8][8], int x, int y) {
    for (int i=0;i<y;i++)
        if (board[x][i] == 1)
            return false;
    for (int i=x,j=y;i>=0 && j>=0;i--,j--)
        if (board[i][j] == 1)
            return false;
    for (int i=x,j=y;j>=0 && i<8;i++,j--)
        if (board[i][j] == 1)
            return false;
    return true;
}
//recursive function to place the queens on the board using backtracking
bool solve(int board[8][8], int col) {
    //base case
    if (col>=8)
        return true;
    for (int i=0;i<8;i++) {
        if (check(board, i, col)) {
            board[i][col] = 1;
            if (solve(board,col+1) == true)
                return true;
            board[i][col] = 0;
        }
    }
    return false;
}
int main() {
    int board[8][8] = {
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0},
            { 0, 0, 0, 0, 0, 0, 0, 0} };
    solve(board, 0);
    for (int i=0;i<8;i++) {
        for (int j=0;j<8;j++)
            printf("%d ", board[i][j]);
        printf("\n");
    }
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to place eight queens on an 8 x 8 chessboard in such a...

    Write a program to place eight queens on an 8 x 8 chessboard in such a way that one queen is to be in each row. A program will use 2 DIMENIONAL array x[r][c] to do this configuration. If x[r] has value c, then in row r there is a queen in column c. Write a program that asks a user to enter the columns that contain queens in the 8 rows. The program then places the queens in these...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...

    Complete the program that solves the Eight Queens problem. The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| Use the Queens class given. In your implementation of the Queens class, complete the body of all methods marked as “To be implemented in Programming Problem 1.” Do not change any of the global variable declarations, constructor or placeQueens methods. Here is what I have so far with notes of what is needed. public class Queens...

  • Complete the program that solves the Eight Queens problem in java only please (pages 318 through...

    Complete the program that solves the Eight Queens problem in java only please (pages 318 through 320). The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| PlaceQueens(in currColumn:integer) //places queens in columns numbered currColumn through 8 If (currColumn>8){ The problem is solved } Else { While(unconsidered squares exist in curr column and the problem is unsolved ){ Determine the next square in column currColumn that is not under attack by a queen in an...

  • Main objective: Solve the n queens problems. You have to place n queens on an n...

    Main objective: Solve the n queens problems. You have to place n queens on an n × n chessboard such that no two attack each other. Important: the chessboard should be indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the square in the 4th column and 3rd row. We have a slight twist in this assignment. We will take as input the position of one queen, and have to generate a solution with a...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the...

    in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide missing logic for the class Queens that will enable it to create a two-dimensional array that...

  • Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n)...

    Can someone please complete the "allTheQueensAreSafe" function? #include <stdio.h> #include <stdlib.h> void printBoard(int *whichRow, int n) {    int row, col;    for (row = 0; row < n; row++)    {        for (col = 0; col < n; col++)        {            printf("%c", (whichRow[col] == row) ? 'Q' : '.');        }        printf("\n");    }    printf("\n"); } int allTheQueensAreSafe(int *whichRow, int n, int currentCol) {    // TODO: Write a function that returns 1 if all the queens represented by    // this array are safe (i.e., none...

  • Artificial Intelligence homework. Please answer correctly. 3. Assume you were asked to place 8 queens on...

    Artificial Intelligence homework. Please answer correctly. 3. Assume you were asked to place 8 queens on an 8x8 chessboard such that no two queens can attack each other (i.e. share the same row, column, or diagonal). Please introduce a solution to use genetic algorithm to solve the game. Please explain the encoding of the state for genetic algorithm (i.e., the representation of the chromosome), the fitness function, the cross over, and the mutation process. [1 pt].

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT