Question

WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The...

WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The program takes the user's input integer for N and prints out all the solutions for N . The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, the following is the output for 4 entered for userinput.

Output for 4 Queens :

1-
 *  *  Q  * 
 Q  *  *  * 
 *  *  *  Q 
 *  Q  *  * 

2-
 *  Q  *  * 
 *  *  *  Q 
 Q  *  *  * 
 *  *  Q  * 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

2 public class Queens Problem { Nm tono int[] queenPositions; boolean solutionPossible = false; <terminated> Queens Problem (

public class QueensProblem {

        int[] queenPositions;
        boolean solutionPossible = false;

        // constructor
        public QueensProblem(int noOfQueens) {
                queenPositions = new int[noOfQueens];
        }

        /**
         * Check if a queen can be placed in row r and column c. When we call this
         * function, the queenPositions array would have been filled for (row -1)
         * rows already
         * return true if placement is possible else false
         */
        public boolean canPlaceQueen(int row, int col) {
                for (int a = 0; a < row; a++) {
                        if (queenPositions[a] == col
                                        || (a - row) == (queenPositions[a] - col)
                                        || (a - row) == (col - queenPositions[a])) {
                                return false;
                        }
                }
                return true;
        }

        // print results in matrix format
        public void printOutputQueens(int[] x) {
                
                // x[i] = y, means in the ith Row, queen need to be placed in yth column
                int noOfQuens = x.length;
                for (int row = 0; row < noOfQuens; row++) {
                        for (int col = 0; col < noOfQuens; col++) {
                                if (x[row] == col) {
                                        System.out.print("Q ");
                                } else {
                                        System.out.print("* ");
                                }
                        }
                        System.out.println();
                }
                System.out.println();
        }

        /**
         * We are using backtracking here. First we will place one queen at a
         * row, then we will recurse for other rows and check to make sure that
         * the queens don't conflict each other. If they do so, we stop and
         * start again
         * 
         */
        public void placeQueensUtil(int rowNo, int noOfQueens) {
                for (int queenNo = 0; queenNo < noOfQueens; queenNo++) {
                        if (canPlaceQueen(rowNo, queenNo)) {
                                queenPositions[rowNo] = queenNo;
                                if (rowNo == noOfQueens - 1) {
                                        // if queen has been placed at last row
                                        solutionPossible = true; // solution for this problem is possible
                                        printOutputQueens(queenPositions);
                                } else {
                                        // recurse for other rows now.
                                        placeQueensUtil(rowNo + 1, noOfQueens);
                                }
                        }
                }
        }

        // this method places the queens at specified positions
        public void placeQueens() {
                
                // for checking if any solution exists 
                solutionPossible = false;
                
                placeQueensUtil(0, queenPositions.length);
                
                if(!solutionPossible) {
                        System.out.println("No Solutions are possible for " + queenPositions.length + " X " + queenPositions.length);
                }
        }

        public static void main(String args[]) {
                QueensProblem queensProblem = new QueensProblem(2);
                queensProblem.placeQueens();

                queensProblem = new QueensProblem(3);
                queensProblem.placeQueens();
                
                queensProblem = new QueensProblem(4);
                queensProblem.placeQueens();

        }
}
Add a comment
Know the answer?
Add Answer to:
WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The...
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
  • 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...

  • Implement the N-Queens algorithm using backtracking to place 8 queens on a 8x8 chess board in...

    Implement the N-Queens algorithm using backtracking to place 8 queens on a 8x8 chess board in a way that no two queens can attack each other. Your output should print out a 8x8 grid of cells with a "1" to indicate a queen placement. JAVA CODE

  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    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" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • 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...

  • 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...

  • The problem Write a program that inputs two integers n and k, where n>=k. Your program...

    The problem Write a program that inputs two integers n and k, where n>=k. Your program should calculate the number of different ways that k bishops could be placed on an nXn chessboard. Structure your program using the backtracking scheme that we have used for the eight queens problem. What needs to be modified is the “OK” function. Input Your main program should loop asking the user for values of n and k. Output Each time through the loop, output...

  • 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...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • Part A Write a Java program that reads an integer n from the keyboard and loops...

    Part A Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised. Part B Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.   Will give thumbs up...

  • ================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...

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
ADVERTISEMENT