Advertisement
uopspop

Untitled

Sep 7th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Backtracking_zerooneknapsack {
  4.     static boolean isDebugMode = false;
  5.  
  6.     public static void main(String[] args)
  7.     {
  8.         double capacity_weight = 10;
  9.         Integer[] weight = new Integer[]{2, 3, 2, 5, 3};
  10.         Integer[] val = new Integer[]{40, 50, 100, 95, 30};
  11.  
  12.         System.out.println("v: ");
  13.         printArray(val);
  14.         System.out.println("w: ");
  15.         printArray(weight);
  16.  
  17.         Integer finalVal = backtracking(capacity_weight, val, weight);
  18.  
  19.         System.out.println("result: ");
  20.         System.out.println(finalVal);
  21.     }
  22.  
  23.     private static Integer backtracking(double capacity_weight, Integer[] vals, Integer[] weights) {
  24.         Integer curWeight = 0;
  25.         Integer curVal = 0;
  26.         Integer curIndex = 0;
  27.         int finalVal = goDownDFS(capacity_weight, vals, weights, curWeight, curVal, curIndex);
  28.         return finalVal;
  29.     }
  30.  
  31.     private static Integer goDownDFS(double capacity_weight, Integer[] vals, Integer[] weights, Integer curWeight, Integer curVal, Integer curIndex) {
  32.         // ending point
  33.         if ( curWeight > capacity_weight) { // backtracking here
  34.             return -1; // 爆了,所以當作最小價值
  35.         }
  36.         if (curIndex >= vals.length) {
  37.             return curVal;
  38.         }
  39.  
  40.         // if take
  41.         int ifTakeWeight = curWeight + weights[curIndex];
  42.         int ifTakeVal = curVal + vals[curIndex];
  43.         int ifTakeCurrVal = goDownDFS(capacity_weight, vals, weights, ifTakeWeight, ifTakeVal, curIndex+1);
  44.  
  45.         // if not take
  46.         int ifNotTakeWeight = curWeight;
  47.         int ifNotTakeVal = curVal;
  48.         int ifNotTakeCUrrVal = goDownDFS(capacity_weight, vals, weights, ifNotTakeWeight, ifNotTakeVal, curIndex+1);
  49.  
  50.         return  Math.max(ifTakeCurrVal, ifNotTakeCUrrVal);
  51.  
  52.     }
  53.  
  54.     ///// Util Only below /////
  55.  
  56.     public static int[][] generateDeepCopy2dAry(int[][] original) {
  57.         if (original == null) {
  58.             return null;
  59.         }
  60.  
  61.         final int[][] result = new int[original.length][];
  62.         for (int i = 0; i < original.length; i++) {
  63.             result[i] = Arrays.copyOf(original[i], original[i].length);
  64.             // For Java versions prior to Java 6 use the next:
  65.             // System.arraycopy(original[i], 0, result[i], 0, original[i].length);
  66.         }
  67.         return result;
  68.     }
  69.  
  70.     private static void printArray(Object[] ary){
  71.         for (int i = 0; i < ary.length; i++) {
  72.             System.out.printf("%4d" , ary[i]);
  73.         }
  74.         System.out.println();
  75.     }
  76.     private static void print2DArray(int[][] ary){
  77.         for (int i = 0; i < ary.length; i++) {
  78.             for (int j = 0; j < ary[i].length; j++) {
  79.                 System.out.printf("%4d" , ary[i][j]);
  80.             }
  81.             System.out.println();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement