Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class NQueens { private int BOARD_SIZE; public static final int EMPTY = 0; public static final int QUEEN = -1; public int board[][]; private int

image text in transcribed

class NQueens { private int BOARD_SIZE;

public static final int EMPTY = 0; public static final int QUEEN = -1; public int board[][]; private int backTracks; private int isAttackedCalls; private int numPlacedQueens; public NQueens(int size) { BOARD_SIZE = size; board = new int[BOARD_SIZE][BOARD_SIZE]; backTracks = 0; isAttackedCalls = 0; numPlacedQueens = 0; } // end constructor public boolean placeQueens() { return placeQueens(0); } private boolean placeQueens(int column) { if (column == BOARD_SIZE) { return true; } else { boolean queenPlaced = false; int row = 0; // number of square in column while ( !queenPlaced && (row Precondition: A queen was placed in this *position and it is being removed as part of the backtracking process.

  • Postcondition: Sets the square on the board in a given row and column to EMPTY. Also unmark all board *positions attacked by this queen and not by any previous queens. Should call markBoard with appropriate arguments @param row where the queen to be removed is located * @param column where the queen to be removed is located */ private void removeQueenAndMarks(int row, int column) { // TO COMPLETE } // end removeQueen private void markBoard (int row, int column, int oldMarker, int newMarker){ markForward(row,column,1,oldMarker,newMarker); } /** Marks the column number col+distance to denote the location that can be attacked by the queen being placed.
  • Precondition: A queen was placed in the position *(row, col) and the (col+distance)th column is being marked as part of the lookahead process.
  • Postcondition: Marks up to three * squares on the board in column (col+distance). These squares are the one on the same row and the ones on the diagonals emanating from the queen placed at (row, col). Marks only *those squares not marked by a previously placed queen. @param row where the new queen is located @param col where the new queen is located @param distance identifies the *state of the markup process: the 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 } private boolean isAttacked(int row, int column) { isAttackedCalls ++; if (board[row][column]!= EMPTY) return true; else return false; } // end isAttacked public String getStatsInHTML(){ return "Statistics for NQueens on a "+BOARD_SIZE+" x "+BOARD_SIZE +" chess board " +"Number of Back Tracks: "+backTracks +" " +"Number of isAttacked() calls : "+isAttackedCalls +" " +"Number of times Queens were placed: "+numPlacedQueens +" "; }} // end NQueens

    NQueensGUI.java

    import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.TableModelListener; import javax.swing.table.*; public class NQueensGUI implements ActionListener{ NQueens nQueens; JFrame boardFrame; JPanel boardPanel; JTextField sizeField; JLabel sizeLabel; JButton placeButton; BoardModel bModel; JTable board; JLabel statusLabel; TableCellRenderer renderer; JLabel statsLabel; public NQueensGUI() { JFrame.setDefaultLookAndFeelDecorated(true); boardFrame = new JFrame("NQueens"); boardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); boardPanel = new JPanel(); boardPanel.setOpaque(true); addWidgets(); boardFrame.getContentPane().add(boardPanel, BorderLayout.CENTER); boardFrame.getRootPane().setDefaultButton(placeButton); boardFrame.setSize(new Dimension(600, 600)); boardFrame.setVisible(true); } public void addWidgets() { sizeField = new JTextField(2); sizeField.setHorizontalAlignment(JTextField.TRAILING); sizeField.setSize(30, 10); sizeLabel = new JLabel("number of queens"); placeButton = new JButton("Place queens"); placeButton.setSize(30, 20); statusLabel = new JLabel(); statusLabel.setSize(40, 10); bModel = new BoardModel(); board = new JTable(bModel); board.setGridColor(Color.CYAN); board.setShowGrid(true); board.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane scrollpane = new JScrollPane(board); renderer = new CustomTableCellRenderer(); statsLabel = new JLabel(); statsLabel.setHorizontalTextPosition(4); board.setDefaultRenderer( Object.class, renderer ); placeButton.addActionListener(this); boardPanel.add(sizeLabel); boardPanel.add(sizeField); boardPanel.add(placeButton); boardPanel.add(statusLabel); boardPanel.add(scrollpane, BorderLayout.CENTER); boardPanel.add(statsLabel); } public void actionPerformed(ActionEvent event) { Object[][] data = null; statusLabel.setText("Computing ..."); bModel.update(data); statsLabel.setText( "" + "


    Computing
    " +"
    "); statusLabel.repaint(); statsLabel.repaint(); board.repaint(); int size = Integer.parseInt(sizeField.getText()); nQueens = new NQueens(size); sizeField.setText(""); long cputime = 0, finish = 0, start = System.currentTimeMillis(); if (nQueens.placeQueens()) { finish = System.currentTimeMillis(); statusLabel.setText("" + " A solution to "+size+" Queens" +""); data = new Object[size][size]; Object[] columnName = new String[size]; cputime = finish - start; for (int i = 0; i " + "CPU time =" +((float) cputime/1000)+ "sec " +nQueens.getStatsInHTML() +"

  • 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

    Database Systems Introduction To Databases And Data Warehouses

    Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

    1st Edition

    1943153191, 978-1943153190

    More Books

    Students also viewed these Databases questions