Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Something's wrong with my code as it does not give me the correct output (the output should match the example output given in the decrption

image text in transcribed

Something's wrong with my code as it does not give me the correct output (the output should match the example output given in the decrption of the problem). Please read the decription of the problem, which is found above. So far my code is as follows:

import java.util.Arrays; /** * . * * @author (dit navn her) * @version (versions nummer eller dato her) */ public class Solver { private int noOfQueens; private int[] queens; private int noOfSolutions;

public static void main(String args[]) {

Solver g = new Solver(6);

g.positionQueens(0);

g.printSolution();

g.findAllSolutions(6); } public Solver(int n) { this.noOfQueens = noOfQueens; this.noOfSolutions = noOfSolutions; queens = new int[n]; } public void findAllSolutions(int noOfQueens) { System.out .println("*****************************************************"); System.out.println(" Solution for " + noOfQueens + " queens:"); System.out.println(""); positionQueens(0); System.out.println(""); System.out.println(" A total of " + noOfSolutions + " solutions were found in " + System.currentTimeMillis() + " ms"); } public void positionQueens(int row) { if (row == noOfQueens) { printSolution(); } for (int i = 0; i Legal positions: "); for (int i = 0; i

ps: i'm writing in Bluej ( A free Java Development Environment designed for beginners), so acturaly i do not need the main metod, but i is added here for convinience.

The FindAllSolutions method is the method that is made available to users Eight queens puzzle in java It updates the field variables, as well as prints some start info on the al(th ist lines In this task we will deal with the so-called a queen problem where you have to place a queens on an Bx B chessboard so that nelther of the queens can beat each other. This means that the single queen must not have other queens in the row, column and the two diagonals that pass through its position. on the following printout) Hereafter, positionQueens is called with the parameter value O, which places the first one *queen and starts the recursion. 1 must place n queens on a n x n More generally, we will deal with the n-queen problem, where n chessboard so that no When the first call to PositionQueens returns, all solutions are found and printed at the terminal, and nothing more than information on the terminal is printed (the last three lines on the following printout). queens can hit each other For more information on the Eight queens puzzle visit: (https:/fen.wikipedia.org/wiki/Eight queens_puzzle) Immediately one can think that the chessboard should be represented by a 2-dimensional array queens new boolean [n] [n], where the Boolean value indicates whether ornot a queen is standing in the field. However, as we know that there can never be more than one queen in each row, it is easier to represent the board by i, 1-dimensional array queens = new int In], where queens li] indicate the position of the queen in row i {ie, queens [3] = S mean that the queen in row 3 is in column 5). As usual, we start the numberin ith D, e. row 0 and colurmn 0. BlueJ: Terminal Window-Queens Solutions for 6 queens b1 d2 f3 a4 c5 e6 c1 f2 b3 e4 a5 d6 d1 a2 e3 b4 f5 c6 e1 c2 a3 f4 d5 b6 Exercise 1: Create a Solver class that has three field variables: Int queens A total of 4 solutions were found in ms as well as five methods, the first of which is public, while the last four are private . void findAllSolutions(int noOfQueens) boolean legalfint row, int col) * void printsolution) String convert int row, int col) The most important method is PositionQueens, which is recursive A call with the parameter value O places a queen in row O, then calling the method with parameter value 1, placing a queen in row 1, then calling the method with parameter value 2, etc. In each step, the legal method is used to check where in the row the queen can be placed. The method returns true if it is legal to place a queen on the position (row, col. When one of the recursive calls has tested all the fields in its row, the call returns and thus, you are back to the previous call, trying to place his queen on the remaining fields in its row. When the recursive method is called with the parameter value noofQueens, one has placed all the queens and the solution found is printed on the terminal using method printsolution, which uses the convert method to convert from (row, col) notation for the usual chess notation for fields, (where (0,0) is written as a1 while (5,3) is written as d6 and(7.7) as h8)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2018 Dublin Ireland September 10 14 2018 Proceedings Part 1 Lnai 11051

Authors: Michele Berlingerio ,Francesco Bonchi ,Thomas Gartner ,Neil Hurley ,Georgiana Ifrim

1st Edition

3030109240, 978-3030109240

More Books

Students also viewed these Databases questions

Question

LO2 Explain the major laws governing employee compensation.

Answered: 1 week ago