Question
You will modify the NQueens.java le to implement a lookahead-based solution to the N-Queens problem. input 4---25 for N for test. I have almost been
You will modify the NQueens.java le to implement a lookahead-based solution to the N-Queens problem. input 4---25 for N for test. I have almost been done with setQueenAndMarks method; removeQueensAndMarks method; markForward method (must be tail-recursive); Comments/style. But it is a little defective. 1.the N larger than 15 cannot get a solution for this problem. 2."oldMarker" in "markForward" is not been used. So, modify my codes for these three methods please.
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) { if(board[row][column]==EMPTY) { board[row][column]=QUEEN; markBoard(row, column, column,column+1 ); } // 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) { if(distance+row < BOARD_SIZE) { if(board[row+distance][col] == EMPTY) board[row+distance][col]= newMarker; } if(distance+col 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