SHARE
TWEET

Untitled

a guest May 19th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.telerikacademy;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ScroogeMcDuck2 {
  8.     private static int[][] matrix;
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         String[] str = reader.readLine().split(" ");
  13.  
  14.         int rows = Integer.parseInt(str[0]);
  15.         int cols = Integer.parseInt(str[1]);
  16.  
  17.         String[][] input = new String[rows][];
  18.  
  19.         for (int i = 0; i < rows; i++) {
  20.             input[i] = reader.readLine().split(" ");
  21.         }
  22.  
  23.         matrix = new int[rows][cols];
  24.  
  25.         int currRow = 0;
  26.         int currCol = 0;
  27.  
  28.         for (int i = 0; i < rows; i++) {
  29.             for (int j = 0; j < cols; j++) {
  30.                 matrix[i][j] = Integer.parseInt(input[i][j]);
  31.                 if (matrix[i][j] == 0) {
  32.                     currCol = j;
  33.                     currRow = i;
  34.                 }
  35.             }
  36.         }
  37.  
  38.         System.out.println(calculateCoins(currRow, currCol));
  39.     }
  40.  
  41.     private static int calculateCoins(int row, int col) {
  42.         int maxCoins = 0;
  43.  
  44.         int nextRow = 0;
  45.         int nextCol = 0;
  46.  
  47.         int[] possibleRows = {-1, 1, 0, 0};
  48.         int[] possibleCols = {0, 0, -1, 1};
  49.  
  50.  
  51.         for (int i = 0; i < possibleRows.length; i++) {
  52.             int x = col + possibleRows[i];
  53.             int z = row + possibleCols[i];
  54.  
  55.             if (x >= 0 && x < matrix[0].length && z >= 0 && z < matrix.length) {
  56.                 int tempCoins = matrix[z][x];
  57.                 if (tempCoins > maxCoins) {
  58.                     nextCol = x;
  59.                     nextRow = z;
  60.                     maxCoins = tempCoins;
  61.                 }
  62.             }
  63.         }
  64.  
  65.         if (maxCoins == 0) return 0;
  66.  
  67.         matrix[nextRow][nextCol]--;
  68.  
  69.         return calculateCoins(nextRow, nextCol) +1;
  70.     }
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top