Advertisement
JackOUT

Untitled

Jan 17th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. package com.bcs2024.knapsack.algorithm.dancinglinks;
  2.  
  3. import com.bcs2024.knapsack.model.CargoSpace;
  4. import com.bcs2024.knapsack.model.Parcel;
  5. import com.bcs2024.knapsack.util.ShapesAndRotations;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class DLSearch {
  11. private ShapesAndRotations shapesAndRotations = new ShapesAndRotations();
  12. public static int length = (int) (CargoSpace.length);
  13. public static int height = (int) (CargoSpace.height);
  14. public static int width = (int) (CargoSpace.width);
  15. public static int totalValue = 0;
  16. // private String[] sequence = {"C", "B", "A"};
  17. private String[] sequence = {"T", "P", "L"};
  18. DancingLinks dance = new DancingLinks(width * height * length);
  19. public static List<ParcelInfo> parcelInfo = new ArrayList<>();
  20. public static int pieceCount = 0;
  21. private int currentPieceValue;
  22.  
  23. public void createPositions() {
  24. int nr = 0;
  25. for (final String type : sequence) {
  26. final Parcel parcel = new Parcel(type);
  27. currentPieceValue = parcel.getValue();
  28. for (int rotation = 0; rotation < shapesAndRotations.rotationNum(type); rotation++) {
  29. final int[][][] shape = shapesAndRotations.getShape(type, rotation);
  30. final int shapeWidth = shape[0][0].length;
  31. final int shapeHeight = shape[0].length;
  32. final int shapeLength = shape.length;
  33. for (int z = 0; z < length; z++) {
  34. for (int y = 0; y < height; y++) {
  35. for (int x = 0; x < width; x++) {
  36. if (!canPlace(x, y, z, shape)) {
  37. continue;
  38. }
  39. final List<Integer> xs = getOccupiedCellsX(shape, x);
  40. final List<Integer> ys = getOccupiedCellsY(shape, y);
  41. final List<Integer> zs = getOccupiedCellsZ(shape, z);
  42.  
  43. int[] positions = null;
  44.  
  45. if (type == "L" || type == "P" || type == "T") {
  46. positions = new int[5];
  47. } else {
  48. positions = new int[shapeLength * shapeHeight * shapeWidth];
  49. }
  50.  
  51. for (int i = 0; i < positions.length; i++) {
  52. positions[i] = length * height * xs.get(i) + length * ys.get(i) + zs.get(i);
  53. }
  54.  
  55. parcelInfo.add(new ParcelInfo(nr, x, y, z, parcel.getId(), shape, currentPieceValue));
  56. dance.AddRow(nr, parcel.getId(), positions, shape);
  57. nr++;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. dance.algorithmX(0);
  64. }
  65.  
  66. public boolean canPlace(final int startX, final int startY, final int startZ, final int[][][] shape) {
  67. final int shapeWidth = shape[0][0].length;
  68. final int shapeHeight = shape[0].length;
  69. final int shapeDepth = shape.length;
  70.  
  71. if (startX + shapeWidth > width) {
  72. return false;
  73. }
  74.  
  75. if (startY + shapeHeight > height) {
  76. return false;
  77. }
  78.  
  79. return startZ + shapeDepth <= length;
  80. }
  81.  
  82. public List<Integer> getOccupiedCellsX(final int[][][] pieceToPlace, final int x0) {
  83. final List<Integer> xs = new ArrayList<>();
  84.  
  85. for (int z = 0; z < pieceToPlace.length; z++) {
  86. for (int y = 0; y < pieceToPlace[0].length; y++) {
  87. for (int x = 0; x < pieceToPlace[0][0].length; x++) {
  88. if (pieceToPlace[z][y][x] != 0) {
  89. xs.add(x + x0);
  90. }
  91. }
  92. }
  93. }
  94. return xs;
  95.  
  96. }
  97.  
  98. public List<Integer> getOccupiedCellsY(final int[][][] pieceToPlace, final int y0) {
  99. final List<Integer> ys = new ArrayList<>();
  100.  
  101. for (int z = 0; z < pieceToPlace.length; z++) {
  102. for (int y = 0; y < pieceToPlace[0].length; y++) {
  103. for (int x = 0; x < pieceToPlace[0][0].length; x++) {
  104. if (pieceToPlace[z][y][x] != 0) {
  105. ys.add(y + y0);
  106. }
  107. }
  108. }
  109. }
  110. return ys;
  111. }
  112.  
  113. public List<Integer> getOccupiedCellsZ(final int[][][] pieceToPlace, final int z0) {
  114. final List<Integer> zs = new ArrayList<>();
  115.  
  116. for (int i = 0; i < pieceToPlace.length; i++) {
  117. for (int j = 0; j < pieceToPlace[0].length; j++) {
  118. for (int k = 0; k < pieceToPlace[0][0].length; k++) {
  119. if (pieceToPlace[i][j][k] != 0) {
  120. zs.add(i + z0);
  121. }
  122. }
  123. }
  124. }
  125. return zs;
  126. }
  127.  
  128.  
  129. public static void main(final String[] args) {
  130. final DLSearch dlx = new DLSearch();
  131. dlx.createPositions();
  132. }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement