Advertisement
bokoness

prince

Jun 9th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. public static int prince(int[][] drm, int i, int j) {
  2.         //TODO: remove
  3.         printPrince(drm, "start");
  4.         return prince(drm, i, j, -1);
  5.     }
  6.  
  7.     public static int prince(int[][] drm, int i, int j, int lastSpot) {
  8.         int count = 0, tempCount = 0, tempCount2 = 0;
  9.         //out of bounders
  10.         if (i < 0 || i >= drm.length || j < 0 || j >= drm.length)
  11.             return -1;
  12.  
  13.         int spot = drm[i][j];
  14.  
  15.         //if the evil man is found
  16.         if (spot == -1)
  17.             return 1;
  18.  
  19.         //already visited
  20.         else if (spot == -2)
  21.             return -1;
  22.  
  23.         //if jump was too high
  24.         else if(lastSpot > 0 && lastSpot > spot && lastSpot - spot > 2)
  25.             return -1;
  26.  
  27.         //if climb was too high
  28.         else if (lastSpot > 0 && lastSpot < spot && spot - lastSpot > 1)
  29.             return -1;
  30.  
  31.  
  32.         int shortestPath = Integer.MAX_VALUE;
  33.         //The comments of go right apply to go up, down right left as well
  34.  
  35.         //Go right
  36.         drm[i][j] = -2; // mark the visited spot with -2
  37.         //TODO: remove
  38.         printPrince(drm, "right");
  39.         count = prince(drm, i , j + 1, spot); //go Right
  40.         count = count == -1 ? -1 : count + 1; // if path didn't lead to dead end, add 1 to count
  41.         shortestPath = count != -1 && count < shortestPath ? count : shortestPath; //if this path is the shortest save its count in shortestPath
  42.         drm[i][j] = spot; //change the visited spot back to its original value
  43.  
  44.         int lastI, lastJ;
  45.  
  46.         //Go up
  47.         drm[i][j] = -2;
  48.         //TODO: remove
  49.         printPrince(drm, "up");
  50.         count = prince(drm, i - 1, j, spot);
  51.         count = count == -1 ? -1 : count + 1;
  52.         shortestPath = count != -1 && count < shortestPath ? count : shortestPath;
  53.         drm[i][j] = spot;
  54.  
  55.         //Go down
  56.         drm[i][j] = -2;
  57.         //TODO: remove
  58.         printPrince(drm, "down");
  59.         count = prince(drm, i + 1, j, spot);
  60.         count = count == -1 ? -1 : count + 1;
  61.         shortestPath = count != -1 && count < shortestPath ? count : shortestPath;
  62.         drm[i][j] = spot;
  63.  
  64.         //Go left
  65.         drm[i][j] = -2;
  66.         //TODO: remove
  67.         printPrince(drm, "left");
  68.         count = prince(drm, i, j - 1, spot);
  69.         count = count == -1 ? -1 : count + 1;
  70.         shortestPath = count != -1 && count < shortestPath ? count : shortestPath;
  71.         drm[i][j] = spot;
  72.  
  73.         return shortestPath == Integer.MAX_VALUE ? -1 : shortestPath;
  74.     }
  75.  
  76.     private static void printPrince(int[][] arr, String direction) {
  77.         System.out.println(" ");
  78.         switch (direction) {
  79.             case "start": System.out.println("Start");break;
  80.             case "left": System.out.println("←"); break;
  81.             case "right": System.out.println("→"); break;
  82.             case "down": System.out.println("↑"); break;
  83.             case "up": System.out.println("↓"); break;
  84.         }
  85.         for(int i = 0; i < arr.length; i ++) {
  86.             for(int j = 0; j < arr.length; j++) {
  87.                 if(arr[i][j] == -1 || arr[i][j] == -2)
  88.                     System.out.print(arr[i][j] + " | ");
  89.                 else
  90.                     System.out.print(" " + arr[i][j] + " | ");
  91.             }
  92.             System.out.println(" ");
  93.         }
  94.         System.out.println(" ");
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement