Advertisement
Guest User

Untitled

a guest
May 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. package Recursion3;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ScroogeMcDuck {
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10. String[] nm = br.readLine().split(" ");
  11. int row = Integer.parseInt(nm[0]);
  12. int col = Integer.parseInt(nm[1]);
  13. int curRow = 0;
  14. int curCol = 0;
  15. int counter = 0;
  16. int[][] matrix = new int[row][col];
  17. for (int i = 0; i < row; i++) {
  18. String[] line = br.readLine().split(" ");
  19. for (int j = 0; j < col; j++) {
  20. matrix[i][j] = Integer.parseInt(line[j]);
  21. if (matrix[i][j] == 0) {
  22. curRow = i;
  23. curCol = j;
  24. }
  25. }
  26. }
  27. System.out.println(count(matrix, curRow, curCol, counter));
  28. }
  29.  
  30. private static int count(int[][] matrix, int curRow, int curCol, int counter) {
  31. int largest = 0;
  32. if (matrix[curRow][curCol] != 0) {
  33. if (isInRange(matrix, curRow, curCol - 1)) {
  34. if (largest < matrix[curRow][curCol - 1]) {
  35. return count(matrix, curRow, curCol - 1, counter = counter + 1);
  36. }
  37. }
  38. if (isInRange(matrix, curRow, curCol + 1)) {
  39. if (largest < matrix[curRow][curCol + 1]) {
  40. return count(matrix, curRow, curCol + 1, counter = counter + 1);
  41. }
  42. }
  43. if (isInRange(matrix, curRow - 1, curCol)) {
  44. if (largest < matrix[curRow -1][curCol]) {
  45. return count(matrix, curRow -1, curCol, counter = counter + 1);
  46. }
  47. }
  48. if (isInRange(matrix, curRow + 1, curCol)) {
  49. if (largest < matrix[curRow +1][curCol]) {
  50. return count(matrix, curRow + 1, curCol, counter = counter + 1);
  51. }
  52. } else {
  53. return 1;
  54. }
  55. } else if (matrix[curRow][curCol] == 0) {
  56. if (isInRange(matrix, curRow, curCol - 1)) {
  57. if (largest < matrix[curRow][curCol - 1]) {
  58. return count(matrix, curRow, curCol - 1, counter);
  59. }
  60. }
  61. if (isInRange(matrix, curRow, curCol + 1)) {
  62. if (largest < matrix[curRow][curCol + 1]) {
  63. return count(matrix, curRow, curCol + 1, counter);
  64. }
  65. }
  66. if (isInRange(matrix, curRow - 1, curCol)) {
  67. if (largest < matrix[curRow -1][curCol]) {
  68. return count(matrix, curRow -1, curCol, counter);
  69. }
  70. }
  71. if (isInRange(matrix, curRow + 1, curCol)) {
  72. if (largest < matrix[curRow +1][curCol]) {
  73. return count(matrix, curRow + 1, curCol, counter);
  74. }
  75. }
  76. } else {
  77. return counter;
  78. }
  79. return counter;
  80. }
  81. private static boolean isInRange(int[][] matrix, int row, int col) {
  82. return row < matrix.length && row >= 0 && col < matrix[0].length && col >= 0;
  83. }
  84. }
  85.  
  86. // 4 3
  87. // 3 2 4
  88. // 2 0 3
  89. // 1 1 5
  90. // 2 2 5
  91. // 22
  92.  
  93. // 3 3
  94. // 10 10 0
  95. // 10 10 10
  96. // 10 10 10
  97. // 78
  98.  
  99. // 3 3
  100. // 10 10 10
  101. // 10 0 10
  102. // 10 10 10
  103. // 80
  104.  
  105. // 2 3
  106. // 0 5 2
  107. // 2 5 3
  108. // 15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement