Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. package com.company;
  2. import java.io.PrintStream;
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. public class Board1 {
  7.  
  8. private int numOfQueens;
  9. private ArrayList<Integer> configuration;
  10. private int[][] conflicts;
  11. private Random random = new Random();
  12. private int queensConflicts = 0;
  13.  
  14. Board1(int numOfQueens) {
  15. this.numOfQueens = numOfQueens;
  16. setConfiguration();
  17. }
  18.  
  19. private void setConfiguration() {
  20. configuration = new ArrayList<>(numOfQueens);
  21. conflicts = new int[numOfQueens][numOfQueens];
  22. queensConflicts = 0;
  23. int randIndexInFirstRow = random.nextInt(numOfQueens);
  24. Coordinates coordinatesOfFirstQueen = new Coordinates(0, randIndexInFirstRow);
  25. configuration.add(randIndexInFirstRow);
  26. updateConflictsForQueen(coordinatesOfFirstQueen, Update.INCREMENT);
  27. int i = 1;
  28. while(i < numOfQueens) {
  29. int indexOfQueen = minConflictsIndexesPerRow(i);
  30. configuration.add(indexOfQueen);
  31. Coordinates coordinatesOfQueen = new Coordinates(i, indexOfQueen);
  32. updateConflictsForQueen(coordinatesOfQueen, Update.INCREMENT);
  33. i++;
  34. }
  35. }
  36.  
  37. private void updateConflictsForQueen(Coordinates queenCoordinates, Update update) {
  38. if(update == Update.INCREMENT) {
  39. int row = queenCoordinates.getX() - 1;
  40. int left = queenCoordinates.getY() - 1;
  41. int above = queenCoordinates.getY();
  42. int right = queenCoordinates.getY() + 1;
  43.  
  44. while (row >= 0) {
  45. if(left >= 0) conflicts[row][left]++;
  46. conflicts[row][above]++;
  47. if(right < numOfQueens) conflicts[row][right]++;
  48.  
  49. left--;
  50. row--;
  51. right++;
  52. }
  53.  
  54. row = queenCoordinates.getX() + 1;
  55. left = queenCoordinates.getY() - 1;
  56. int down = queenCoordinates.getY();
  57. right = queenCoordinates.getY() + 1;
  58.  
  59. while (row < numOfQueens) {
  60. if(left >= 0) conflicts[row][left]++;
  61. conflicts[row][down]++;
  62. if(right < numOfQueens) conflicts[row][right]++;
  63.  
  64. left--;
  65. row++;
  66. right++;
  67. }
  68. }
  69. else {
  70. int row = queenCoordinates.getX() - 1;
  71. int left = queenCoordinates.getY() - 1;
  72. int above = queenCoordinates.getY();
  73. int right = queenCoordinates.getY() + 1;
  74.  
  75. while (row >= 0) {
  76. if(left >= 0) conflicts[row][left]--;
  77. conflicts[row][above]--;
  78. if(right < numOfQueens) conflicts[row][right]--;
  79.  
  80. left--;
  81. row--;
  82. right++;
  83. }
  84.  
  85. row = queenCoordinates.getX() + 1;
  86. left = queenCoordinates.getY() - 1;
  87. int down = queenCoordinates.getY();
  88. right = queenCoordinates.getY() + 1;
  89.  
  90. while (row < numOfQueens) {
  91. if(left >= 0) conflicts[row][left]--;
  92. conflicts[row][down]--;
  93. if(right < numOfQueens) conflicts[row][right]--;
  94.  
  95. left--;
  96. row++;
  97. right++;
  98. }
  99. }
  100. }
  101.  
  102. void solve() {
  103. int moves = 0;
  104. int restart = 0;
  105.  
  106. while(true) {
  107. int worstQueenIndex = findMostConflicted();
  108. if(queensConflicts == 0) {
  109. System.out.println("Move: " + moves + " Restarts: " + restart);
  110. return;
  111. }
  112. int minConflictIndex = minConflictsIndexesPerRow(worstQueenIndex);
  113. moveQueen(worstQueenIndex, minConflictIndex);
  114. if(moves == 100 || minConflictIndex == -1) {
  115. setConfiguration();
  116. moves = 0;
  117. restart++;
  118. }
  119. moves++;
  120. }
  121. }
  122.  
  123. public void print(PrintStream stream) {
  124. for(int i = 0; i < configuration.size(); i++) {
  125. for(int j = 0; j < configuration.size(); j++) {
  126. stream.print(configuration.get(i) == j ? "* " : "_ ");
  127. }
  128. stream.println();
  129. }
  130. }
  131.  
  132. private void moveQueen(int rowOfQueen, int indexOfMin) {
  133. Coordinates oldPlaceQueen = new Coordinates(rowOfQueen, configuration.get(rowOfQueen));
  134. Coordinates newPlaceQueen = new Coordinates(rowOfQueen, indexOfMin);
  135. //Not to move queen and the same place
  136. if(!oldPlaceQueen.equalCoordinates(newPlaceQueen) && indexOfMin != -1) {
  137. configuration.set(rowOfQueen, indexOfMin);
  138. updateConflictsForQueen(newPlaceQueen, Update.INCREMENT);
  139. updateConflictsForQueen(oldPlaceQueen, Update.DECREMENT);
  140. }
  141. }
  142.  
  143. private int minConflictsIndexesPerRow(int row) {
  144. ArrayList<Integer> minConflicted = new ArrayList<>();
  145. int minConflicts = numOfQueens;
  146. for (int col = 0; col < numOfQueens; col++) {
  147. int numberOfConflicts = conflicts[row][col];
  148. if (numberOfConflicts == minConflicts) {
  149. minConflicted.add(col);
  150. }
  151. else if (numberOfConflicts < minConflicts) {
  152. minConflicts = numberOfConflicts;
  153. minConflicted.clear();
  154. minConflicted.add(col);
  155. }
  156. }
  157. //if the row has no min conflicts, we have no option to move the queen
  158. if(minConflicted.size() == numOfQueens - 1) {
  159. return -1;
  160. }
  161. int index = random.nextInt(minConflicted.size());
  162. return minConflicted.get(index);
  163. }
  164.  
  165. private int findMostConflicted() {
  166. ArrayList<Integer> maxConflicted = new ArrayList<>();
  167. int maxConflicts = 0;
  168. queensConflicts = 0;
  169. for (int row = 0; row < numOfQueens; row++) {
  170. int numberOfConflicts = conflicts[row][configuration.get(row)];
  171. if (numberOfConflicts == maxConflicts) {
  172. maxConflicted.add(row);
  173. }
  174. else if (numberOfConflicts > maxConflicts) {
  175. maxConflicts = numberOfConflicts;
  176. queensConflicts = maxConflicts;
  177. maxConflicted.clear();
  178. maxConflicted.add(row);
  179. }
  180. }
  181. int index = random.nextInt(maxConflicted.size());
  182. return maxConflicted.get(index);
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement