Advertisement
LiliyaBurlakova

Untitled

Dec 9th, 2019
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[]args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] size = scanner.nextLine().split(" ");
  10.         int rows = Integer.parseInt(size[0]);
  11.         int columns = Integer.parseInt(size[1]);
  12.  
  13.         int[][] matrix = new int[rows][columns];
  14.  
  15.         int currentRow = 0;
  16.         int currentCol = 0;
  17.  
  18.         for (int r = 0; r < rows; r++) {
  19.             String[] line = scanner.nextLine().split(" ");
  20.             for (int c = 0; c < line.length; c++) {
  21.                 matrix[r][c] = Integer.parseInt(line[c]);
  22.                 if (matrix[r][c] == 0) {
  23.                     currentRow = r;
  24.                     currentCol = c;
  25.                 }
  26.             }
  27.         }
  28.         int one = 0;
  29.         int two = 0;
  30.         int three = 0;
  31.         int four = 0;
  32.         int coins = 0;
  33.         int curr = 0;
  34.  
  35.         for (int r = 0; r < rows; r++) {
  36.             for (int c = 0; c < columns; c++) {
  37.                 if (currentCol - 1 >= 0) {
  38.                     one = matrix[currentRow][currentCol - 1];
  39.                 }
  40.                 if (currentCol + 1 < columns) {
  41.                     two = matrix[currentRow][currentCol + 1];
  42.                 }
  43.                 if (currentRow - 1 >= 0) {
  44.                     three = matrix[currentRow - 1][currentCol];
  45.                 }
  46.                 if (currentRow + 1 < columns) {
  47.                     four = matrix[currentRow + 1][currentCol];
  48.                 }
  49.                 if (one >= two && one >= three && one >= four) {
  50.                     currentCol = currentCol - 1;
  51.                     curr = matrix[currentRow][currentCol];
  52.                     curr--;
  53.                     matrix[currentRow][currentCol] = curr;
  54.                     coins++;
  55.                 } else if (two > one && two >= three && two >= four) {
  56.                     currentCol = currentCol + 1;
  57.                     curr = matrix[currentRow][currentCol];
  58.                     curr--;
  59.                     matrix[currentRow][currentCol] = curr;
  60.                     coins++;
  61.                 } else if (three > one && three > two && three >= four) {
  62.                     currentRow = currentRow - 1;
  63.                     curr = matrix[currentRow][currentCol];
  64.                     curr--;
  65.                     matrix[currentRow][currentCol] = curr;
  66.                     coins++;
  67.                 } else if (four > one && four > two && four > three) {
  68.                     currentRow = currentRow + 1;
  69.                     curr = matrix[currentRow][currentCol];
  70.                     curr--;
  71.                     matrix[currentRow][currentCol] = curr;
  72.                     coins++;
  73.                 }
  74.  
  75.                 if (one == 0 && two == 0 && three == 0 && four == 0) {
  76.                     System.out.println(coins);
  77.                     return;
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement