Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ScroogeMcDuck2 {
- private static int[][] matrix;
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String[] str = reader.readLine().split(" ");
- int rows = Integer.parseInt(str[0]);
- int cols = Integer.parseInt(str[1]);
- String[][] input = new String[rows][];
- for (int i = 0; i < rows; i++) {
- input[i] = reader.readLine().split(" ");
- }
- matrix = new int[rows][cols];
- int currRow = 0;
- int currCol = 0;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- matrix[i][j] = Integer.parseInt(input[i][j]);
- if (matrix[i][j] == 0) {
- currCol = j;
- currRow = i;
- }
- }
- }
- System.out.println(calculateCoins(currRow, currCol));
- }
- private static int calculateCoins(int row, int col) {
- int maxCoins = 0;
- int nextRow = 0;
- int nextCol = 0;
- int[] possibleRows = {-1, 1, 0, 0};
- int[] possibleCols = {0, 0, -1, 1};
- for (int i = 0; i < possibleRows.length; i++) {
- int x = col + possibleRows[i];
- int z = row + possibleCols[i];
- if (x >= 0 && x < matrix[0].length && z >= 0 && z < matrix.length) {
- int tempCoins = matrix[z][x];
- if (tempCoins > maxCoins) {
- nextCol = x;
- nextRow = z;
- maxCoins = tempCoins;
- }
- }
- }
- if (maxCoins == 0) return 0;
- matrix[nextRow][nextCol]--;
- return calculateCoins(nextRow, nextCol) +1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement