Advertisement
jig487

Weight Method - CS220

Apr 25th, 2024
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. /**
  2.      * Calculates the weight being carried by the person at row, col.
  3.      *
  4.      * @param row The row position of the person.
  5.      * @param col The column position of the person.
  6.      * @return A double representing the amount of weight the person at row col is carrying.
  7.      */
  8.     private static double weightOn(int row, int col) {
  9.         if (row == 0 && col == 0) {
  10.             //base case: we are at (0,0)
  11.             return 0;
  12.         } else if (col == 0) {
  13.             //recursive case: left edge
  14.             //(weight of the person above + the weight they are carrying) divide by 2
  15.             return (weightOn(row-1, col) + weight) / 2;
  16.         } else if (row == col) {
  17.             //recursive case: right edge
  18.             //(weight of the person above + the weight they are carrying) divide by 2
  19.             return (weightOn(row-1, col-1) + weight) / 2;
  20.         } else {
  21.             //recursive case: somewhere in the middle
  22.             //(weight of the people above + the weight they are carrying) divide by 2
  23.             double leftNode = weightOn(row-1,col-1);
  24.             double rightNode = weightOn(row-1,col);
  25.  
  26.             return (leftNode + rightNode + weight*2) / 2;
  27.         }
  28.     }
  29.  
  30.     /**
  31.      * Calculate the weight being carried by ther person at row, col.
  32.      * Uses memoization for optimization
  33.      * RETURNS: A 2d ragged array of weights, filled out for the given row and column
  34.      *
  35.      * @param row The row position of the person.
  36.      * @param col The column position of the person.
  37.      * @return A double representing the amount of weight the person at row col is carrying.
  38.      */
  39.     private static double[][] weightOnMemo(int row, int col, double[][] arr) {
  40.         //initialize ragged array and fill with 0.0 if arr is empty
  41.         if (arr.length == 0) {
  42.             arr = new double[row+1][];
  43.             for (int r = 0; r < arr.length; r++ ) {
  44.                 arr[r] = new double[r+1];
  45.                 for (int c = 0; c < r+1; c++) {
  46.                     arr[r][c] = 0.0;
  47.                 }
  48.             }
  49.         }
  50.        
  51.         //check if value already exists
  52.         if (arr[row][col] != 0.0) {
  53.             //value already exists
  54.             return arr;
  55.         }
  56.        
  57.         //  Do calculation and recursive method calls.
  58.         //Past here, we know the value for arr[row][col] does not exist so we need to calculate and insert it
  59.         if (col == 0) {
  60.             //left edge
  61.             //check if value above exists
  62.             double val = arr[row-1][col]; //(weightOn(row-1, col) + weight) / 2;
  63.             if (val == 0 && row-1 > 0) {
  64.                 //if val is empty and we are not checking (0,0)
  65.                 arr = weightOnMemo(row-1, col, arr); //fill target index with weight value
  66.                 val = arr[row-1][col]; //update val with new weight
  67.             }
  68.             //fill value for current index
  69.             arr[row][col] = (val+weight)/2;
  70.             return arr;
  71.            
  72.         } else if (row == col) {
  73.             //right edge
  74.             //check if value above exists
  75.             double val = arr[row-1][col-1]; //(weightOn(row-1, col-1) + weight) / 2;
  76.             if (val == 0 && row-1 > 0) {
  77.                 //if val is empty and we are not checking (0,0)
  78.                 arr = weightOnMemo(row-1, col-1, arr); //fill target index with weight value
  79.                 val = arr[row-1][col-1]; //update val with new weight
  80.             }
  81.             //fill value for current index
  82.             arr[row][col] = (val+weight)/2;
  83.             return arr;
  84.                
  85.         } else {
  86.             //middle: need both left and right weights above
  87.             //At this point, we know we cannot check (0,0) directly from out position
  88.             double left = arr[row-1][col-1];
  89.             double right = arr[row-1][col];
  90.  
  91.             if (left == 0) {
  92.                 //if val is empty
  93.                 arr = weightOnMemo(row-1, col-1, arr); //fill target index with weight value
  94.                 left = arr[row-1][col-1]; //update val with new weight
  95.             }
  96.  
  97.             if (right == 0) {
  98.                 //if val is empty
  99.                 arr = weightOnMemo(row-1, col, arr); //fill target index with weight value
  100.                 right = arr[row-1][col]; //update val with new weight
  101.             }
  102.  
  103.             //fill value for current index
  104.             arr[row][col] = (left+right+weight*2)/2;
  105.             return arr;
  106.         }
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement