Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyClass
- {
- public static void main(String args[])
- {
- int[][] matrix = { {0,1,4,2},
- {-2,0,5,7},
- {-8,-1,0,3},
- {-3,-9,-6,0}};
- int[][] matrix2 = {{0,1,5,2},
- {-2,2,5,7},
- {-8,-1,0,+3},
- {-3,-9,-6,0}};
- //boolean isFit = executeSolution1(matrix2,0,0,0);
- boolean isFit = executeSolution2(matrix2,0,0);
- if (isFit)
- System.out.println("\nYeaaaaaaaaaa\n");
- else
- System.out.println("\nNope :(\n");
- }
- public static boolean executeSolution1(int [][] mat, int dx, int dy,int index)
- {
- if ((dx >= mat.length) || (dy>= mat[0].length))
- return true;
- if (validLine(mat,dx,dy,0))
- return executeSolution1(mat,dx+1, dy+1,index+1);
- return false;
- }
- public static boolean validLine(int [][] mat, int dx, int dy,int index)
- {
- if (index >= mat.length)
- return true;
- if (index < dx)
- if (mat[dy][index] < 0)
- return validLine(mat,dx,dy,index+1);
- else
- return false;
- if (index > dx)
- if (mat[dy][index] > 0)
- return validLine(mat,dx,dy,index+1);
- else
- return false;
- else // dx==index
- return validLine(mat,dx,dy,index+1);
- }
- public static boolean executeSolution2(int [][] mat, int currX, int currY)
- {
- if ((currX == mat.length-1) || (currY== mat[0].length-1))
- if (mat[currY][currX] == 0)
- return true;
- else
- return false;
- if ((mat[currY][currX] == 0) &&
- rowFit(mat,currX+1,currY) &&
- columnFit(mat,currX,currY+1) )
- return executeSolution2(mat,currX+1,currY+1);
- return false;
- }
- public static boolean rowFit(int [][] mat, int currX, int currY)
- {
- if (currX == mat.length)
- return true;
- if (mat[currY][currX] > 0)
- return rowFit(mat, currX+1, currY);
- return false;
- }
- public static boolean columnFit(int [][] mat, int currX, int currY)
- {
- if (currY == mat.length)
- return true;
- if (mat[currY][currX] < 0)
- return columnFit(mat, currX, currY+1);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement