public class Main { //Number of rows or columns public static final int BOARD_SIZE = 8; boolean[][] board; //Indicate an empty square public static final boolean EMPTY = false; //Indicate a square which containing a queen public static final boolean QUEEN = true; //Number of moves public static final int MOVES = 4; //Horizontal moves int[] horizontal; //Vertical moves int[] vertical; public int queens = 0; Main main = new Main(); main.placeQueens(0); main.displayBoard(); } public Main() { //Constructor creates an empty board board = new boolean[BOARD_SIZE][BOARD_SIZE]; for (int row = 0; row < board.length; row++) { for (int col = 0; col < board[row].length; col++) { board[row][col] = EMPTY; } } horizontal = new int[MOVES]; vertical = new int[MOVES]; // up right horizontal[0] = -1; vertical[0] = 1; // down left horizontal[1] = 1; vertical[1] = -1; // up left horizontal[2] = -1; vertical[2] = -1; // down right horizontal[3] = 1; vertical[3] = 1; } public boolean placeQueens (int column) { if (column >= BOARD_SIZE) { return true; } else { boolean queenPlaced = false; int row = 0; while (!queenPlaced && row < BOARD_SIZE) { if (isUnderAttack(row, column)) { ++row; }// end if else{ setQueen(row, column); queenPlaced = placeQueens(column + 1); if (!queenPlaced) { removeQueen(row,column); ++row; }// end if }// end else }// end while return queenPlaced; }// end else } private void removeQueen(int row, int column) { board[row][column] = EMPTY; --queens; } private void setQueen(int row, int column) { board[row][column] = QUEEN; ++queens; } public boolean isUnderAttack(int row, int col) { boolean condition = false; // check row for (int column = 0; column < BOARD_SIZE; column++) { if ((board[row][column] == true)) { condition = true; } } // check column for (int row_ = 0; row_ < board.length; row_++) { if (board[row_][col] == true) { condition = true; } } // check diagonal for (int row_ = row, col_ = col; row_ >= 0 && col_ < 8; row_ += horizontal[0], col_ += vertical[0]) { if (board[row_][col_] == true) { condition = true; } } for (int row_ = row, col_ = col; row_ < 8 && col_ >= 0; row_ += horizontal[1], col_ += vertical[1]) { if (board[row_][col_] == true) { condition = true; } } for (int row_ = row, col_ = col; row_ >= 0 && col_ >= 0; row_ += horizontal[2], col_ += vertical[2]) { if (board[row_][col_] == true) { condition = true; } } for (int row_ = row, col_ = col; row_ < 8 && col_ < 8; row_ += horizontal[3], col_ += vertical[3]) { if (board[row_][col_] == true) { condition = true; } } return condition; } public void displayBoard () { int counter = 0; for (int row = 0; row < board.length; row++) { for (int col = 0; col < board[row].length; col++) { if (board[row][col] == true) { counter++; } else { } } } } }
Standard input is empty
queen PLACED in [0][0] queen PLACED in [2][1] queen PLACED in [4][2] queen PLACED in [1][3] queen PLACED in [3][4] queen REMOVED from [3][4] queen PLACED in [7][4] queen REMOVED from [7][4] queen REMOVED from [1][3] queen PLACED in [6][3] queen PLACED in [1][4] queen PLACED in [3][5] queen PLACED in [5][6] queen REMOVED from [5][6] queen REMOVED from [3][5] queen REMOVED from [1][4] queen PLACED in [3][4] queen REMOVED from [3][4] queen REMOVED from [6][3] queen PLACED in [7][3] queen PLACED in [1][4] queen PLACED in [3][5] queen PLACED in [5][6] queen REMOVED from [5][6] queen REMOVED from [3][5] queen REMOVED from [1][4] queen PLACED in [3][4] queen REMOVED from [3][4] queen REMOVED from [7][3] queen REMOVED from [4][2] queen PLACED in [5][2] queen PLACED in [1][3] queen PLACED in [6][4] queen PLACED in [4][5] queen REMOVED from [4][5] queen REMOVED from [6][4] queen REMOVED from [1][3] queen PLACED in [7][3] queen PLACED in [1][4] queen PLACED in [3][5] queen REMOVED from [3][5] queen PLACED in [4][5] queen REMOVED from [4][5] queen REMOVED from [1][4] queen REMOVED from [7][3] queen REMOVED from [5][2] queen PLACED in [6][2] queen PLACED in [1][3] queen PLACED in [3][4] queen PLACED in [7][5] queen REMOVED from [7][5] queen REMOVED from [3][4] queen PLACED in [7][4] queen PLACED in [4][5] queen REMOVED from [4][5] queen REMOVED from [7][4] queen REMOVED from [1][3] queen REMOVED from [6][2] queen PLACED in [7][2] queen PLACED in [1][3] queen PLACED in [3][4] queen REMOVED from [3][4] queen PLACED in [6][4] queen REMOVED from [6][4] queen REMOVED from [1][3] queen PLACED in [5][3] queen PLACED in [1][4] queen REMOVED from [1][4] queen PLACED in [3][4] queen PLACED in [1][5] queen PLACED in [4][6] queen REMOVED from [4][6] queen REMOVED from [1][5] queen REMOVED from [3][4] queen REMOVED from [5][3] queen REMOVED from [7][2] queen REMOVED from [2][1] queen PLACED in [3][1] queen PLACED in [1][2] queen PLACED in [4][3] queen PLACED in [2][4] queen REMOVED from [2][4] queen PLACED in [7][4] queen REMOVED from [7][4] queen REMOVED from [4][3] queen PLACED in [6][3] queen PLACED in [2][4] queen REMOVED from [2][4] queen REMOVED from [6][3] queen PLACED in [7][3] queen PLACED in [2][4] queen PLACED in [6][5] queen REMOVED from [6][5] queen REMOVED from [2][4] queen PLACED in [5][4] queen PLACED in [2][5] queen REMOVED from [2][5] queen REMOVED from [5][4] queen REMOVED from [7][3] queen REMOVED from [1][2] queen PLACED in [5][2] queen PLACED in [2][3] queen REMOVED from [2][3] queen PLACED in [7][3] queen PLACED in [1][4] queen PLACED in [4][5] queen PLACED in [2][6] queen REMOVED from [2][6] queen REMOVED from [4][5] queen PLACED in [6][5] queen PLACED in [2][6] queen REMOVED from [2][6] queen REMOVED from [6][5] queen REMOVED from [1][4] queen PLACED in [2][4] queen PLACED in [4][5] queen REMOVED from [4][5] queen PLACED in [6][5] queen REMOVED from [6][5] queen REMOVED from [2][4] queen REMOVED from [7][3] queen REMOVED from [5][2] queen PLACED in [6][2] queen PLACED in [2][3] queen PLACED in [5][4] queen PLACED in [1][5] queen PLACED in [4][6] queen REMOVED from [4][6] queen REMOVED from [1][5] queen REMOVED from [5][4] queen PLACED in [7][4] queen PLACED in [1][5] queen PLACED in [4][6] queen REMOVED from [4][6] queen REMOVED from [1][5] queen REMOVED from [7][4] queen REMOVED from [2][3] queen PLACED in [4][3] queen PLACED in [1][4] queen REMOVED from [1][4] queen PLACED in [2][4] queen REMOVED from [2][4] queen PLACED in [7][4] queen PLACED in [1][5] queen REMOVED from [1][5] queen REMOVED from [7][4] queen REMOVED from [4][3] queen REMOVED from [6][2] queen PLACED in [7][2] queen PLACED in [2][3] queen REMOVED from [2][3] queen PLACED in [4][3] queen PLACED in [1][4] queen REMOVED from [1][4] queen PLACED in [2][4] queen REMOVED from [2][4] queen REMOVED from [4][3] queen REMOVED from [7][2] queen REMOVED from [3][1] queen PLACED in [4][1] queen PLACED in [1][2] queen PLACED in [5][3] queen PLACED in [2][4] queen PLACED in [6][5] queen PLACED in [3][6] queen REMOVED from [3][6] queen REMOVED from [6][5] queen REMOVED from [2][4] queen REMOVED from [5][3] queen PLACED in [7][3] queen PLACED in [2][4] queen PLACED in [6][5] queen PLACED in [3][6] queen REMOVED from [3][6] queen REMOVED from [6][5] queen REMOVED from [2][4] queen PLACED in [5][4] queen PLACED in [2][5] queen REMOVED from [2][5] queen PLACED in [3][5] queen REMOVED from [3][5] queen REMOVED from [5][4] queen REMOVED from [7][3] queen REMOVED from [1][2] queen PLACED in [6][2] queen PLACED in [1][3] queen PLACED in [3][4] queen PLACED in [7][5] queen REMOVED from [7][5] queen REMOVED from [3][4] queen PLACED in [5][4] queen PLACED in [2][5] queen REMOVED from [2][5] queen PLACED in [7][5] queen REMOVED from [7][5] queen REMOVED from [5][4] queen REMOVED from [1][3] queen REMOVED from [6][2] queen PLACED in [7][2] queen PLACED in [1][3] queen PLACED in [3][4] queen PLACED in [6][5] queen PLACED in [2][6] queen REMOVED from [2][6] queen REMOVED from [6][5] queen REMOVED from [3][4] queen PLACED in [6][4] queen PLACED in [2][5] queen PLACED in [5][6] queen REMOVED from [5][6] queen REMOVED from [2][5] queen REMOVED from [6][4] queen REMOVED from [1][3] queen PLACED in [5][3] queen PLACED in [2][4] queen PLACED in [6][5] queen PLACED in [1][6] queen PLACED in [3][7] |x||o||o||o||o||o||o||o| |o||o||o||o||o||o||x||o| |o||o||o||o||x||o||o||o| |o||o||o||o||o||o||o||x| |o||x||o||o||o||o||o||o| |o||o||o||x||o||o||o||o| |o||o||o||o||o||x||o||o| |o||o||x||o||o||o||o||o| 8 queens has been placed