Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Recursion3;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ScroogeMcDuck {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String[] nm = br.readLine().split(" ");
- int row = Integer.parseInt(nm[0]);
- int col = Integer.parseInt(nm[1]);
- int curRow = 0;
- int curCol = 0;
- int counter = 0;
- int[][] matrix = new int[row][col];
- for (int i = 0; i < row; i++) {
- String[] line = br.readLine().split(" ");
- for (int j = 0; j < col; j++) {
- matrix[i][j] = Integer.parseInt(line[j]);
- if (matrix[i][j] == 0) {
- curRow = i;
- curCol = j;
- }
- }
- }
- System.out.println(count(matrix, curRow, curCol, counter));
- }
- private static int count(int[][] matrix, int curRow, int curCol, int counter) {
- int largest = 0;
- if (matrix[curRow][curCol] != 0) {
- if (isInRange(matrix, curRow, curCol - 1)) {
- if (largest < matrix[curRow][curCol - 1]) {
- return count(matrix, curRow, curCol - 1, counter = counter + 1);
- }
- }
- if (isInRange(matrix, curRow, curCol + 1)) {
- if (largest < matrix[curRow][curCol + 1]) {
- return count(matrix, curRow, curCol + 1, counter = counter + 1);
- }
- }
- if (isInRange(matrix, curRow - 1, curCol)) {
- if (largest < matrix[curRow -1][curCol]) {
- return count(matrix, curRow -1, curCol, counter = counter + 1);
- }
- }
- if (isInRange(matrix, curRow + 1, curCol)) {
- if (largest < matrix[curRow +1][curCol]) {
- return count(matrix, curRow + 1, curCol, counter = counter + 1);
- }
- } else {
- return 1;
- }
- } else if (matrix[curRow][curCol] == 0) {
- if (isInRange(matrix, curRow, curCol - 1)) {
- if (largest < matrix[curRow][curCol - 1]) {
- return count(matrix, curRow, curCol - 1, counter);
- }
- }
- if (isInRange(matrix, curRow, curCol + 1)) {
- if (largest < matrix[curRow][curCol + 1]) {
- return count(matrix, curRow, curCol + 1, counter);
- }
- }
- if (isInRange(matrix, curRow - 1, curCol)) {
- if (largest < matrix[curRow -1][curCol]) {
- return count(matrix, curRow -1, curCol, counter);
- }
- }
- if (isInRange(matrix, curRow + 1, curCol)) {
- if (largest < matrix[curRow +1][curCol]) {
- return count(matrix, curRow + 1, curCol, counter);
- }
- }
- } else {
- return counter;
- }
- return counter;
- }
- private static boolean isInRange(int[][] matrix, int row, int col) {
- return row < matrix.length && row >= 0 && col < matrix[0].length && col >= 0;
- }
- }
- // 4 3
- // 3 2 4
- // 2 0 3
- // 1 1 5
- // 2 2 5
- // 22
- // 3 3
- // 10 10 0
- // 10 10 10
- // 10 10 10
- // 78
- // 3 3
- // 10 10 10
- // 10 0 10
- // 10 10 10
- // 80
- // 2 3
- // 0 5 2
- // 2 5 3
- // 15
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement