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 *
![2 public class Queens Problem { Nm tono int[] queenPositions; boolean solutionPossible = false; <terminated> Queens Problem (](http://img.homeworklib.com/questions/f2b7bcd0-27bf-11ec-939c-077cf625693f.png?x-oss-process=image/resize,w_560)
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();
}
}WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The...
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 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 (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 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...
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...
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: |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...
Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...
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...
Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...