Question
You will modify the NQueens.java le to implement a lookahead-based solution to the N-Queens problem. 1. Implement the methods in the le that are not
You will modify the NQueens.java le to implement a lookahead-based solution to the N-Queens problem. 1. Implement the methods in the le that are not implemented and any other helper methods you might use. 2. Add code to write out the number of backtracks (number of times a queen is removed from the board), the number of calls to isAttacked and placeQueens. What is needed to modify: setQueenAndMarks method; removeQueensAndMarks method; markForward method (must be tail-recursive); Comments/style.
class NQueens { /**squares per row or column */ private int BOARD_SIZE; /** indicate an empty square */ public static final int EMPTY = 0; /**indicate square contains a queen */ public static final int QUEEN = -1; /** chess board: each entry board[i][j]
of the board * can take the following values: *
i>0
: where i
number of queens that can * attack the cell (i,j)
*/ public int board[][]; /** current number of backtracks */ private int backTracks; /** number of locations that have been checked to be under attack * at some point in the search process */ private int isAttackedCalls; /** current number of placed queens */ private int numPlacedQueens; /** creates an empty square board */ public NQueens(int size) { BOARD_SIZE = size; board = new int[BOARD_SIZE][BOARD_SIZE]; backTracks = 0; isAttackedCalls = 0; numPlacedQueens = 0; } // end constructor /** * Places queens by calling PlaceQueens with the first column. * @return If a solution is found, each column of the board * contains one queen and method returns true; otherwise, * returns false (no solution exists for a queen anywhere * in column specified). */ public boolean placeQueens() { return placeQueens(0); } /** * Places queens in the specified column of the board and * recursvily place the queens on the successive columns when * possible * @param column where the new queen is added. It is assumed that * queens are correctly placed in columns 1
through * column-1
* @return If a solution is found, each column of the board * contains one queen and method returns true; otherwise, returns * false (no solution exists for a queen anywhere in column * specified). */ private boolean placeQueens(int column) { if (column == BOARD_SIZE) { return true; // base case } else { boolean queenPlaced = false; int row = 0; // number of square in column while ( !queenPlaced && (row < BOARD_SIZE) ) { // if square can be attacked if (isAttacked(row, column)) { ++row; // consider next row in column } // end if else { // place queen and consider next column setQueenAndMarks(row, column); queenPlaced = placeQueens(column+1); // if no queen can be placed in the next column, if (!queenPlaced) { // backtrack: remove queen placed earlier // and try next row in column removeQueenAndMarks(row, column); ++row; ++backTracks; } // end if } // end else } // end while return queenPlaced; } // end else } // end placeQueens /** * Sets a queen at the location indicated by the specified row and * column and marks the columns attacked by this queen and no * other queen placed in prior columns. * Should call markBoard with appropriate arguments * @param row where the new queen is located * @param column where the new queen is located */ private void setQueenAndMarks(int row, int column) { // TO COMPLETE } // end setQueen /** * Removes a queen at the location indicated by row and column, * and marks in the following columns to denote attack by this * queen. *
/** * Marks the column number col+distance
to denote the * location that can be attacked by the queen being placed.
col+distance
column will be marked up * @param oldMarker identifies the old marker * @param newMarker identifies the new marker */ private void markForward (int row, int col, int distance, int oldMarker, int newMarker) { // TO COMPLETE } /** * Determines whether the square on the board at a given row and * column is under attack by any queens in the columns 1 through * column-1. Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started