fork(16) download
  1. public class Main {
  2. //Number of rows or columns
  3. public static final int BOARD_SIZE = 8;
  4.  
  5. boolean[][] board;
  6. //Indicate an empty square
  7. public static final boolean EMPTY = false;
  8. //Indicate a square which containing a queen
  9. public static final boolean QUEEN = true;
  10. //Number of moves
  11. public static final int MOVES = 4;
  12. //Horizontal moves
  13. int[] horizontal;
  14. //Vertical moves
  15. int[] vertical;
  16.  
  17. public int queens = 0;
  18.  
  19. public static void main(String[] s) {
  20. Main main = new Main();
  21. main.placeQueens(0);
  22. main.displayBoard();
  23. }
  24. public Main() {
  25. //Constructor creates an empty board
  26. board = new boolean[BOARD_SIZE][BOARD_SIZE];
  27. for (int row = 0; row < board.length; row++) {
  28. for (int col = 0; col < board[row].length; col++) {
  29. board[row][col] = EMPTY;
  30. }
  31. }
  32.  
  33. horizontal = new int[MOVES];
  34. vertical = new int[MOVES];
  35. // up right
  36. horizontal[0] = -1;
  37. vertical[0] = 1;
  38. // down left
  39. horizontal[1] = 1;
  40. vertical[1] = -1;
  41. // up left
  42. horizontal[2] = -1;
  43. vertical[2] = -1;
  44. // down right
  45. horizontal[3] = 1;
  46. vertical[3] = 1;
  47.  
  48.  
  49. }
  50.  
  51. public boolean placeQueens (int column) {
  52.  
  53. if (column >= BOARD_SIZE) {
  54. return true;
  55. }
  56. else {
  57. boolean queenPlaced = false;
  58. int row = 0;
  59.  
  60. while (!queenPlaced && row < BOARD_SIZE) {
  61. if (isUnderAttack(row, column)) {
  62. ++row;
  63. }// end if
  64. else{
  65. setQueen(row, column);
  66. queenPlaced = placeQueens(column + 1);
  67. if (!queenPlaced) {
  68. removeQueen(row,column);
  69. ++row;
  70. }// end if
  71. }// end else
  72. }// end while
  73. return queenPlaced;
  74. }// end else
  75. }
  76.  
  77.  
  78. private void removeQueen(int row, int column) {
  79. board[row][column] = EMPTY;
  80. System.out.printf("queen REMOVED from [%d][%d]\n", row, column);
  81. --queens;
  82. }
  83.  
  84. private void setQueen(int row, int column) {
  85. board[row][column] = QUEEN;
  86. System.out.printf("queen PLACED in [%d][%d]\n", row, column);
  87. ++queens;
  88.  
  89. }
  90.  
  91. public boolean isUnderAttack(int row, int col) {
  92.  
  93. boolean condition = false;
  94. // check row
  95. for (int column = 0; column < BOARD_SIZE; column++) {
  96. if ((board[row][column] == true)) {
  97. condition = true;
  98. }
  99. }
  100.  
  101. // check column
  102. for (int row_ = 0; row_ < board.length; row_++) {
  103. if (board[row_][col] == true) {
  104. condition = true;
  105. }
  106. }
  107.  
  108. // check diagonal
  109. for (int row_ = row, col_ = col; row_ >= 0 && col_ < 8; row_ += horizontal[0], col_ += vertical[0]) {
  110. if (board[row_][col_] == true) {
  111. condition = true;
  112. }
  113. }
  114. for (int row_ = row, col_ = col; row_ < 8 && col_ >= 0; row_ += horizontal[1], col_ += vertical[1]) {
  115. if (board[row_][col_] == true) {
  116. condition = true;
  117. }
  118. }
  119. for (int row_ = row, col_ = col; row_ >= 0 && col_ >= 0; row_ += horizontal[2], col_ += vertical[2]) {
  120. if (board[row_][col_] == true) {
  121. condition = true;
  122. }
  123. }
  124. for (int row_ = row, col_ = col; row_ < 8 && col_ < 8; row_ += horizontal[3], col_ += vertical[3]) {
  125. if (board[row_][col_] == true) {
  126. condition = true;
  127. }
  128. }
  129.  
  130. return condition;
  131. }
  132.  
  133. public void displayBoard () {
  134. int counter = 0;
  135. for (int row = 0; row < board.length; row++) {
  136. for (int col = 0; col < board[row].length; col++) {
  137. if (board[row][col] == true) {
  138. System.out.printf("|%s|", "x");
  139. counter++;
  140. }
  141. else {
  142. System.out.printf("|%s|", "o");
  143. }
  144. }
  145. System.out.println();
  146.  
  147. }
  148.  
  149. System.out.printf("%d queens has been placed\n", counter);
  150. }
  151. }
  152.  
  153.  
Success #stdin #stdout 0.09s 245888KB
stdin
Standard input is empty
stdout
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