Question

CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses rerecursively locate a square on the next row down, where a queen can be placed, without being attacked by or attacking) a prevare attacked by the blue queen, are shaded blue, and no subsequent queens can be placed on those squares For instance, afterhttps:/loeis.org/A000170 for links to some of this work. The algorithm we are studying in this project is practical for value1 Solutions are encoded as an n-tuple where the ith element is the column number of the queen residing in row i. For instancestatic void placeQueen (int[] [] B, int i, int j) and static void removeQuee (int[l[i B, int i, int j) The first function inc

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 game of Chess a queen can move any number of spaces in any linear direction: horizontally, vertically, or along a diagonal. The Eight Queens puzzle is to find a placement of 8 queens on an otherwise empty 8 X 8 chessboard in such a way that no two queens confront each other. In particular, no two queens lie on the same row column or diagonal. One solution to this problem is pictured below. 曹 The n-Queens problem is the natural generalization, in which n queens are placed on an n x n chessboard in such a way that no two queens lie on the same row, column or diagonal. There are many ways of solving this problem, some of which are described in the Wikipedia article linked above. Our algorithm will
recursively locate a square on the next row down, where a queen can be placed, without being attacked by or attacking) a previously placed queen. The illustration below is a partial box trace of the casen-4 At the top (level 0 of the recursion, we have placed no queens, illustrated by an empty 4 x 4 chessboard. Notice that initially, there are no restrictions on where a queen can be placed on row 1. At level 1 of the recursion therefore, we place a queen on each of the 4 safe positions in row 1. These 4 queen placements generate 4 separate recursive calls. 0 21 -2 12 In the above illustration we have color-coded the queens, so that thc queen on row 1 is bluc, that on row 2 is red, that on row 3 is green and the queen on row 4 is black. For each queen placement, it is necessary to keep track of which squares it attacks on the rows below it. To this end, the squares on rows 2, 3 and 4 that
are attacked by the blue queen, are shaded blue, and no subsequent queens can be placed on those squares For instance, after placing a blue queen on row 1, column 2, we have: Observe that there is only one safe square in row 2, namely square (2, 4). Therefore the placement of a queen on (1, 2) generates just one recursive call, and this box has only one child in the box trace, in which a red queen is placed on (2. 4). The squares on rows 3 and 4 that are attacked by the red queen are of course shaded red. Note however that two of these squares, (3, 4) and (4, 2), are attacked by both the red qucen and the blue queen, and are accordingly shaded purple (since red+blue-purple) 4 In what follows, we'll see that it is most important to keep track of the number of queens attacking a given square from a row above. We signify this in the box trace by placing the negative of that number in the given square. Thus (3, 2) contains -1 since it is attacked by only the blue queen, (3, 3) contains -1 since it is attacked by only the red queen, and (3, 4) contains -2 since it is attacked by both If we succeed in placing a queen on row 4, then we have found a solution to 4-queens, and the algorithm returns value 1 from that box. If the next row contains no safe squares, then the box generates no recursive calls, and returns 0. For instance, after placing a red queen on (2, 2) below, row 3 has no safe square on which to place a queen. We signify this by placing an X under the dead-end box. 3 2-1 In general, each box returns the sum of the returned values of each of its children. As an exercise, complete the box trace on the preceding page by determining the children of the boxes with the symbol : under them. (Don't try to determine the colors, which were given here only for illustration, but do try to determine the number of queens attacking each square from above.) As a further (more challenging) exercise, try to construct a complete box trace for the case n 5, which has 10 solutions. Let a(n) denote the number of solutions to the n-Queens problem. Much research has been done on the sequence (an)o which begins (1, 1,0,0, 2, 10,4,40,92,352,724,2680, 14200.,... See the article
https:/loeis.org/A000170 for links to some of this work. The algorithm we are studying in this project is practical for values oín up lo n 15, or 16 al most. Beyond that, it slows significantly. (I managed to compute a (16) = 14,772,512 in under 2 minutes on my Windows machine.) The iterative algorithm we studied in CMPS 12A was useful only up to about n-13 machines The iterative algentin Program Operation Your program for this project will be called Queens.java. You will include a Makefile that creates an executable Jar file called Queens, allowing one to run the program by typing Queens at the command line Your program will read an integer n from the command line, indicating the size of the Queens problem to solve. The program will operate in two modes: normal and verbose (which is indicated by the command line option "-v"). In normal mode, the program prints only the number of solutions to n-Queens. In verbose mode, all solutions to n-Queens will be printed in the order they are found by the algorithm, and in a format described below, followed by the number of such solutions. Thus to find the number of solutions to 8- Queens you will type: Queens 8 To print all 92 unique solutions to 8-Queens type: % Queens -v 8 If the user types anything on the command line other than the option -v and a number n, the program will print a usage message to stderr and quit. A sample session is included below (remember that & is the unix prompt and you do not type it.) Queens Usage Queens [-v] number Option- verbose output, print all solutions % Queens blan Usage: Queens [-v number Option: -v verbose output, print all solutions % Queens 4 4-Queens has 2 solutions Queens -v 4 4-Queens has 2 solutions % Queens -v 5 5-Queens has 10 solutions
1 Solutions are encoded as an n-tuple where the ith element is the column number of the queen residing in row i. For instance, in the case n-4, the 4-tuple (2, 4, 1, 3) encodes the solution: 2 3 4 and the 4-tuple (3, 1, 4, 2) encodes the other solution: 4 Observe that these solutions are given in the order in which they would be found by our algorithm, as illustrated by the (partial) box trace on page 2 of this document, provided we work from left to right in each row. It is recommended that you write helper functions to perform basic subtasks such as printing the usage message then quitting Program Representation of the Chessboard Your program will represent an n x n chessboard by a 2-dimensional int array of size (n + 1) x (n +1) The extra row and column are there so row and column numbers in the array correspond directly with row and column numbers on the chessboard. Specifically, if the array is called B, then B[il ] corresponds to the square in row i, column j of the chessboard. The following encoding of chessboard states will be used. For 1sisn and 1 sjSn, 1 if square (i,j) contains a queen 0 B [i][j] if square (i,j) is empty, and not under attack from any square above it k if square (i,j) is under attack from k queens lying above it In this context, "above" means "having a smaller row number". Furthermore, since column 0 does not correspond to anything on the chessboard, we will use it to encode a solution in the required format. For 1 i nand j = 0, 0 if rowi is contains no queen e(i][0]=įj İf square (i,j) contains a queen Thus, to print out a solution, enter a loop that prints. (B 1] [0], B [2] [0],昨] [0], of row 0 are not specified, so you can put anything you like in B[o][0n] , BIn] [0]). The contents You may wish to write a helper function to initialize this array to all zeros, though this is not required. Your program will contain two functions with the following headings:
static void placeQueen (int[] [] B, int i, int j) and static void removeQuee (int[l[i B, int i, int j) The first function increments B[i]lj] from its initial value of 0 to , and sets BiI0] to j, thus indicating the existence of a queen on square (Lj). It will also decrement B [k]띠 for every square (k, l) under attack from the new queen at (i, j), where i
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class NQueenProblem{

final int N =8;

void printSolution(int boade[])

{

for (int i=0;i<N;i++){

for(int j=0;j<N;j++){

System.out.print(" "+board[i])[j] + " ");

}

boolean isSafe(int board[][],int row,int col)

{

int i,j;

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

{

if(board[i][j]==1)

return false;

for(i=row; j=col;j>= 0 && i< N;i++,j--)

if(board[i][j]==1)

return false;

return true;

}

boolean solveNQUtil(int board[][], int col){

if(col>=N)

return true;

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

if( isSafe(board,i,col)

board[i][col]=1;

if( solveNQUtil(board,col+1)==true)

return true;

board[i][col]=0;

}

return false'

}

boolean solveNQ()

{

int board[][]= { { 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} };

if(solveNQUtil(board,0)== false) {

System.out.print("Solution does not exist");

return false;}

printSolution(board);

return true;

}

public static void main(String args[])

{

NQueenProblem Queen = new NQuuenProblem();

Queen.solveNQ();

}

}

  

Add a comment
Know the answer?
Add Answer to:
CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...
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
  • 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...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

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

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

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

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

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

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

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

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

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

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