Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[]args) {
- Scanner scanner = new Scanner(System.in);
- String[] size = scanner.nextLine().split(" ");
- int rows = Integer.parseInt(size[0]);
- int columns = Integer.parseInt(size[1]);
- int[][] matrix = new int[rows][columns];
- int currentRow = 0;
- int currentCol = 0;
- for (int r = 0; r < rows; r++) {
- String[] line = scanner.nextLine().split(" ");
- for (int c = 0; c < line.length; c++) {
- matrix[r][c] = Integer.parseInt(line[c]);
- if (matrix[r][c] == 0) {
- currentRow = r;
- currentCol = c;
- }
- }
- }
- int one = 0;
- int two = 0;
- int three = 0;
- int four = 0;
- int coins = 0;
- int curr = 0;
- for (int r = 0; r < rows; r++) {
- for (int c = 0; c < columns; c++) {
- if (currentCol - 1 >= 0) {
- one = matrix[currentRow][currentCol - 1];
- }
- if (currentCol + 1 < columns) {
- two = matrix[currentRow][currentCol + 1];
- }
- if (currentRow - 1 >= 0) {
- three = matrix[currentRow - 1][currentCol];
- }
- if (currentRow + 1 < columns) {
- four = matrix[currentRow + 1][currentCol];
- }
- if (one >= two && one >= three && one >= four) {
- currentCol = currentCol - 1;
- curr = matrix[currentRow][currentCol];
- curr--;
- matrix[currentRow][currentCol] = curr;
- coins++;
- } else if (two > one && two >= three && two >= four) {
- currentCol = currentCol + 1;
- curr = matrix[currentRow][currentCol];
- curr--;
- matrix[currentRow][currentCol] = curr;
- coins++;
- } else if (three > one && three > two && three >= four) {
- currentRow = currentRow - 1;
- curr = matrix[currentRow][currentCol];
- curr--;
- matrix[currentRow][currentCol] = curr;
- coins++;
- } else if (four > one && four > two && four > three) {
- currentRow = currentRow + 1;
- curr = matrix[currentRow][currentCol];
- curr--;
- matrix[currentRow][currentCol] = curr;
- coins++;
- }
- if (one == 0 && two == 0 && three == 0 && four == 0) {
- System.out.println(coins);
- return;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement