Advertisement
bokoness

המבחן האחרון 2020א שאלות 1 ו 2

Jun 29th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1.  
  2. /**
  3.  * The last Mavo test | 2020A
  4.  */
  5. public class LastTest {
  6.     /**
  7.      * Question 1
  8.      */
  9.     public static int minDiagSnake(int[][] mat) {
  10.         if (mat.length <= 1)
  11.             return 0;
  12.         return minDiagSnakeHelper(mat, 0, 0, -1);
  13.     }
  14.  
  15.     private static int minDiagSnakeHelper(int[][] mat, int i, int j, int prev) {
  16.         int height = mat.length - 1;
  17.         int width = mat[0].length - 1;
  18.         int current, diff, count = -1, min = Integer.MAX_VALUE, topRight = Integer.MAX_VALUE, topLeft = Integer.MAX_VALUE, botRight = Integer.MAX_VALUE, botLeft = Integer.MAX_VALUE;
  19.  
  20.         //out of boundaries
  21.         if (i < 0 || j < 0 || i > height || j > width)
  22.             return -1;
  23.  
  24.         current = mat[i][j];
  25.         diff = current > prev ? current - prev : prev - current;
  26.         //if climb or jump were too high
  27.         if (prev >= 0 && diff > 1)
  28.             return -1;
  29.  
  30.         //if already visit
  31.         if (current == -2)
  32.             return -1;
  33.  
  34.         //if snake reached destination
  35.         if (i == height && j == width)
  36.             return 1;
  37.  
  38.         //go bottom right
  39.         mat[i][j] = -2;
  40.         count = minDiagSnakeHelper(mat, i + 1, j + 1, current);
  41.         if (count != -1)
  42.             botRight = 1 + count;
  43.         mat[i][j] = current;
  44.  
  45.         //go bottom left
  46.         mat[i][j] = -2;
  47.         count = minDiagSnakeHelper(mat, i + 1, j - 1, current);
  48.         if (count != -1)
  49.             botLeft = 1 + count;
  50.         mat[i][j] = current;
  51.  
  52.         //go top right
  53.         mat[i][j] = -2;
  54.         count = minDiagSnakeHelper(mat, i - 1, j + 1, current);
  55.         if (count != -1)
  56.             topRight = 1 + count;
  57.         mat[i][j] = current;
  58.  
  59.         //go top left
  60.         mat[i][j] = -2;
  61.         count = minDiagSnakeHelper(mat, i - 1, j - 1, current);
  62.         if (count != -1)
  63.             topLeft += 1 + count;
  64.         mat[i][j] = current;
  65.  
  66.         min = Math.min(Math.min(topRight, topLeft), Math.min(botRight, botLeft));
  67.         return min == Integer.MAX_VALUE ? -1 : min;
  68.     }
  69.  
  70.     /**
  71.      * Question 2
  72.      */
  73.     public static int minSumDeadEndSnake(int[][] mat) {
  74.         if (mat.length <= 1)
  75.             return 0;
  76.         return minSumDeadEndSnakeHelper(mat, 0, 0, -1);
  77.     }
  78.  
  79.     private static int minSumDeadEndSnakeHelper(int[][] mat, int i, int j, int prev) {
  80.         int height = mat.length - 1;
  81.         int width = mat[0].length - 1;
  82.         int current, diff = 0, count = -1, min = Integer.MAX_VALUE, up = Integer.MAX_VALUE, left = Integer.MAX_VALUE, down = Integer.MAX_VALUE, right = Integer.MAX_VALUE;
  83.  
  84.         // i + 1 <= height && mat[i+1][j] != -2
  85.         if(i < 0 || j < 0 || i > height || j > width)
  86.             return -1;
  87.  
  88.         current = mat[i][j];
  89.         if (prev != -1)
  90.             diff = current > prev ? current - prev : prev - current;
  91.  
  92.         //if jump or climb are too high
  93.         if (diff > 1)
  94.             return -1;
  95.  
  96.         if(i == height && j == width)
  97.             return current;
  98.  
  99.         //go down
  100.         mat[i][j] = -2;
  101.         count = minSumDeadEndSnakeHelper(mat, i + 1, j, current);
  102.         down = count >= 0 ? count + current : down;
  103.         mat[i][j] = current;
  104.  
  105.         //go left
  106.         mat[i][j] = -2;
  107.         count = minSumDeadEndSnakeHelper(mat, i, j - 1, current);
  108.         left = count >= 0 ? count + current : left;
  109.         mat[i][j] = current;
  110.         //go up
  111.         mat[i][j] = -2;
  112.         count = minSumDeadEndSnakeHelper(mat, i - 1, j, current);
  113.         up = count >= 0 ? count + current : up;
  114.         mat[i][j] = current;
  115.  
  116.         //go right
  117.         mat[i][j] = -2;
  118.         count = minSumDeadEndSnakeHelper(mat, i, j + 1, current);
  119.         right = count >= 0 ? count + current : right;
  120.         mat[i][j] = current;
  121.  
  122.         min = Math.min(Math.min(up, down), Math.min(right, left));
  123.         return min == Integer.MAX_VALUE ? current : min;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement