Advertisement
JackOUT

Untitled

Jan 17th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. package com.bcs2024.knapsack.algorithm;
  2.  
  3. import com.bcs2024.knapsack.model.CargoSpace;
  4. import com.bcs2024.knapsack.model.Cell;
  5. import com.bcs2024.knapsack.model.Header;
  6. import com.bcs2024.knapsack.model.ParcelInfo;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Stack;
  11.  
  12. public class DancingLinks {
  13.  
  14. public Header root;
  15. public Header[] headers;
  16. public Stack<Integer> answer;
  17. public Stack<Integer> pentIDS;
  18. public static int length = (int) (CargoSpace.length * 2);
  19. public static int height = (int) (CargoSpace.height * 2);
  20. public static int width = (int) (CargoSpace.width * 2);
  21. public static boolean stop = false;
  22. public static int[][][] field;
  23. private CargoSpace cargoSpace;
  24.  
  25. public DancingLinks(final int columns) {
  26. cargoSpace = new CargoSpace();
  27. answer = new Stack<>();
  28. pentIDS = new Stack<>();
  29.  
  30. root = new Header(-1);
  31. headers = new Header[columns];
  32. for (int j = 0; j < columns; j++) {
  33. headers[j] = new Header(j);
  34. root.InsertLeft(headers[j]);
  35. }
  36.  
  37. field = new int[width][height][length];
  38. }
  39.  
  40. public void AddRow(final int row, final int pentId, final int[] ones, final int[][][] piece) {
  41. final int last = -1;
  42. Cell first = null;
  43. for (final int x : ones) {
  44. final Cell cell = new Cell(headers[x]);
  45. headers[x].InsertUp(cell);
  46. cell.row = row;
  47. cell.shape = piece;
  48. cell.pentID = pentId;
  49.  
  50. headers[x].size++;
  51.  
  52. if (x <= last) {
  53. throw new IllegalArgumentException("Column indexes must be in increasing order");
  54. }
  55.  
  56. if (first == null) {
  57. first = cell;
  58. } else {
  59. first.InsertLeft(cell);
  60. }
  61. }
  62. }
  63.  
  64. public int countA = 0;
  65. public int countB = 0;
  66. public int countC = 0;
  67.  
  68. public void algorithmX(final int step) {
  69. if (stop) return;
  70. final List<ParcelInfo> parcelInfo = new ArrayList<>();
  71. if (answer.size() >= 10) {
  72. DLSearch.pieceCount = 0;
  73. DLSearch.totalValue = 0;
  74. for (final var ans : answer) {
  75. final ParcelInfo r = DLSearch.parcelInfo.get(ans);
  76. parcelInfo.add(r);
  77. }
  78.  
  79. clearField();
  80.  
  81. for (final var info : parcelInfo) {
  82. cargoSpace.placeParcel(info.shape, info.x0, info.y0, info.z0, field);
  83. DLSearch.pieceCount++;
  84. DLSearch.totalValue += info.pieceValue;
  85. switch (info.parcelID) {
  86. case 1 -> countA++;
  87. case 2 -> countB++;
  88. case 3 -> countC++;
  89. }
  90. }
  91. System.out.println("Total value: " + DLSearch.totalValue);
  92. System.out.println(countA + " " + countB + " " + countC);
  93. if (countA != 0 || countB != 0 || countC != 0) {
  94. if (DLSearch.totalValue >= 228) {
  95. stop = true;
  96. return;
  97. }
  98. } else if (root.R == root) {
  99. stop = true;
  100. return;
  101. }
  102. countA = 0;
  103. countB = 0;
  104. countC = 0;
  105. field = new int[width][height][length];
  106. }
  107.  
  108. Header head = (Header) root.R;
  109. int minSize = head.size;
  110. for (Cell xCell = head; xCell != root; xCell = xCell.R) {
  111. if (((Header) xCell).size < minSize) {
  112. minSize = ((Header) xCell).size;
  113. head = (Header) xCell;
  114.  
  115. if (head.C.size == 0) {
  116. return;
  117. }
  118. }
  119. }
  120. cover(head);
  121. for (Cell rCell = head.D; rCell != head; rCell = rCell.D) {
  122. answer.push(rCell.row);
  123. pentIDS.push(rCell.pentID);
  124.  
  125. for (Cell jCell = rCell.R; jCell != rCell; jCell = jCell.R) {
  126. cover(jCell.C);
  127. }
  128. algorithmX(step + 1);
  129. answer.pop();
  130. pentIDS.pop();
  131.  
  132. for (Cell jCell = rCell.L; jCell != rCell; jCell = jCell.L) {
  133. uncover(jCell.C);
  134. }
  135.  
  136. }
  137. uncover(head);
  138. }
  139.  
  140. private void clearField() {
  141. for (int i = 0; i < field.length; i++) {
  142. for (int j = 0; j < field[0].length; j++) {
  143. for (int k = 0; k < field[0][0].length; k++) {
  144. field[i][j][k] = -1;
  145. }
  146. }
  147. }
  148. }
  149.  
  150. private void cover(final Header head) {
  151. head.R.L = head.L;
  152. head.L.R = head.R;
  153.  
  154. for (Cell iCell = head.D; iCell != head; iCell = iCell.D) {
  155. for (Cell jCell = iCell.R; iCell != jCell; jCell = jCell.R) {
  156. jCell.D.U = jCell.U;
  157. jCell.U.D = jCell.D;
  158. jCell.C.size--;
  159. }
  160. }
  161. }
  162.  
  163. private void uncover(final Header head) {
  164. for (Cell iCell = head.U; iCell != head; iCell = iCell.U)
  165. for (Cell jCell = iCell.L; jCell != iCell; jCell = jCell.L) {
  166. jCell.D.U = jCell;
  167. jCell.U.D = jCell;
  168. jCell.C.size++;
  169. }
  170. head.R.L = head;
  171. head.L.R = head;
  172. }
  173.  
  174. }
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement