Advertisement
Guest User

awsomeeeeeeeeeeee

a guest
Dec 12th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1.  
  2. public class Ex14
  3. {
  4.     public static final int MARK = -666;
  5.    
  6.     public static void main(String [] args)
  7.     {
  8.        int[] arr = {2,4,1,6,4,2,4,3,5};
  9.        int[] arr2 = {1,4,3,1,2,4,3};
  10.        int[] arr3 = {1,1,1,3,2,2,4,1};
  11.        if (isWay(arr3))
  12.            System.out.println("There is a way");
  13.        else
  14.            System.out.println("No way");
  15.     }
  16.        
  17.        
  18.     // ############Question 1 Section###############
  19.     public static boolean isWay(int[] a)
  20.     {
  21.         return isWay(a, 0);
  22.     }
  23.    
  24.     public static boolean isWay(int[] a, int currInd)
  25.     {
  26.         boolean canGoRight, canGoLeft;
  27.         int currValue = a[currInd];
  28.        
  29.         if (currInd == a.length-1)
  30.             return true;
  31.            
  32.         if (a[currInd] == MARK)
  33.             return false;
  34.        
  35.         a[currInd] = MARK;
  36.        
  37.         if (currInd + currValue<a.length)
  38.             canGoRight = isWay(a, currValue+currInd);
  39.         else
  40.             canGoRight = false;
  41.            
  42.         if (currInd - currValue>=0)
  43.             canGoLeft = isWay(a,currInd - currValue);
  44.         else
  45.             canGoLeft = false;
  46.        
  47.         a[currInd] = currValue;
  48.         return (canGoRight||canGoLeft);
  49.     }
  50.    
  51.     // ############Question 2 Section###############
  52.     public static boolean isHeal(int[][] mat, int x, int y)
  53.     {
  54.        
  55.        // Each one of the corners of the N x M array.
  56.        if ((x == 0) && (y == 0)) // top left (0,0)
  57.             return ( (mat[y][x] > mat[y][x+1]) && (mat[y][x] > mat[y+1][x]) );
  58.            
  59.        if ((x == mat[0].length-1) && (y == mat.length-1)) // down right (N,M)
  60.             return ( (mat[y][x] > mat[y][x-1]) && (mat[y][x] > mat[y-1][x]) );
  61.        
  62.        if ((x == mat[0].length-1) && (y == 0)) //top right (N,0)
  63.             return ( (mat[y][x] > mat[y][x-1]) && (mat[y][x] > mat[y+1][x]) );
  64.            
  65.        if ((x == 0) && (y ==  mat.length-1)) //down left (0,M)
  66.             return ( (mat[y][x] > mat[y][x+1]) && (mat[y][x] > mat[y-1][x]) );
  67.            
  68.        //Each one of the borderlines.              
  69.        if ((x == 0) && (y != 0) && ((y != mat.length-1))) //left border with the highest and lowest elements.
  70.             return ( (mat[y][x] > mat[y][x+1]) && (mat[y][x] > mat[y-1][x])&& (mat[y][x] > mat[y+1][x]) );
  71.            
  72.     }
  73.    
  74.    
  75.    
  76.    
  77.    
  78.    
  79.    
  80.    
  81.    
  82.    
  83.    
  84.    
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.    
  92.    
  93.    
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement