Advertisement
JackOUT

Untitled

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