Advertisement
Guest User

amazinggggggggggggggg

a guest
Dec 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. public class MyClass
  2. {
  3. public static void main(String args[])
  4. {
  5. int[][] matrix = { {0,1,4,2},
  6. {-2,0,5,7},
  7. {-8,-1,0,3},
  8. {-3,-9,-6,0}};
  9.  
  10. int[][] matrix2 = {{0,1,5,2},
  11. {-2,2,5,7},
  12. {-8,-1,0,+3},
  13. {-3,-9,-6,0}};
  14.  
  15. //boolean isFit = executeSolution1(matrix2,0,0,0);
  16. boolean isFit = executeSolution2(matrix2,0,0);
  17. if (isFit)
  18. System.out.println("\nYeaaaaaaaaaa\n");
  19. else
  20. System.out.println("\nNope :(\n");
  21. }
  22.  
  23. public static boolean executeSolution1(int [][] mat, int dx, int dy,int index)
  24. {
  25. if ((dx >= mat.length) || (dy>= mat[0].length))
  26. return true;
  27.  
  28. if (validLine(mat,dx,dy,0))
  29. return executeSolution1(mat,dx+1, dy+1,index+1);
  30. return false;
  31. }
  32.  
  33. public static boolean validLine(int [][] mat, int dx, int dy,int index)
  34. {
  35. if (index >= mat.length)
  36. return true;
  37. if (index < dx)
  38. if (mat[dy][index] < 0)
  39. return validLine(mat,dx,dy,index+1);
  40. else
  41. return false;
  42. if (index > dx)
  43. if (mat[dy][index] > 0)
  44. return validLine(mat,dx,dy,index+1);
  45. else
  46. return false;
  47. else // dx==index
  48. return validLine(mat,dx,dy,index+1);
  49. }
  50.  
  51.  
  52. public static boolean executeSolution2(int [][] mat, int currX, int currY)
  53. {
  54. if ((currX == mat.length-1) || (currY== mat[0].length-1))
  55. if (mat[currY][currX] == 0)
  56. return true;
  57. else
  58. return false;
  59.  
  60. if ((mat[currY][currX] == 0) &&
  61. rowFit(mat,currX+1,currY) &&
  62. columnFit(mat,currX,currY+1) )
  63. return executeSolution2(mat,currX+1,currY+1);
  64.  
  65. return false;
  66. }
  67.  
  68. public static boolean rowFit(int [][] mat, int currX, int currY)
  69. {
  70. if (currX == mat.length)
  71. return true;
  72. if (mat[currY][currX] > 0)
  73. return rowFit(mat, currX+1, currY);
  74. return false;
  75. }
  76.  
  77. public static boolean columnFit(int [][] mat, int currX, int currY)
  78. {
  79. if (currY == mat.length)
  80. return true;
  81. if (mat[currY][currX] < 0)
  82. return columnFit(mat, currX, currY+1);
  83. return false;
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement