Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ex14
- {
- public static final int MARK = -666;
- public static void main(String [] args)
- {
- int[] arr = {2,4,1,6,4,2,4,3,5};
- int[] arr2 = {1,4,3,1,2,4,3};
- int[] arr3 = {1,1,1,3,2,2,4,1};
- if (isWay(arr3))
- System.out.println("There is a way");
- else
- System.out.println("No way");
- }
- // ############Question 1 Section###############
- public static boolean isWay(int[] a)
- {
- return isWay(a, 0);
- }
- public static boolean isWay(int[] a, int currInd)
- {
- boolean canGoRight, canGoLeft;
- int currValue = a[currInd];
- if (currInd == a.length-1)
- return true;
- if (a[currInd] == MARK)
- return false;
- a[currInd] = MARK;
- if (currInd + currValue<a.length)
- canGoRight = isWay(a, currValue+currInd);
- else
- canGoRight = false;
- if (currInd - currValue>=0)
- canGoLeft = isWay(a,currInd - currValue);
- else
- canGoLeft = false;
- a[currInd] = currValue;
- return (canGoRight||canGoLeft);
- }
- // ############Question 2 Section###############
- public static boolean isHeal(int[][] mat, int x, int y)
- {
- // Each one of the corners of the N x M array.
- if ((x == 0) && (y == 0)) // top left (0,0)
- return ( (mat[y][x] > mat[y][x+1]) && (mat[y][x] > mat[y+1][x]) );
- if ((x == mat[0].length-1) && (y == mat.length-1)) // down right (N,M)
- return ( (mat[y][x] > mat[y][x-1]) && (mat[y][x] > mat[y-1][x]) );
- if ((x == mat[0].length-1) && (y == 0)) //top right (N,0)
- return ( (mat[y][x] > mat[y][x-1]) && (mat[y][x] > mat[y+1][x]) );
- if ((x == 0) && (y == mat.length-1)) //down left (0,M)
- return ( (mat[y][x] > mat[y][x+1]) && (mat[y][x] > mat[y-1][x]) );
- //Each one of the borderlines.
- if ((x == 0) && (y != 0) && ((y != mat.length-1))) //left border with the highest and lowest elements.
- return ( (mat[y][x] > mat[y][x+1]) && (mat[y][x] > mat[y-1][x])&& (mat[y][x] > mat[y+1][x]) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement