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 ScroogeMcDuck {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = br.readLine();
- String[] dimensions = line.split(" ");
- int N = Integer.parseInt(dimensions[0]);
- int M = Integer.parseInt(dimensions[1]);
- int[][] matrix = new int[N][M];
- int curX = 0;
- int curY = 0;
- int coins = 0;
- for (int i = 0; i < N; i++) {
- String lines = br.readLine();
- String[] nLines = lines.split(" ");
- for (int j = 0; j < M; j++) {
- matrix[i][j] = Integer.parseInt(nLines[j]);
- if (matrix[i][j] == 0) {
- curX = i;
- curY = j;
- System.out.println("filling the current possition: " + i + " " + j);
- }
- }
- }
- System.out.println("printing matrix now: ");
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- for (int i = 1; i < M - 1; i++) {
- for (int j = 1; j < N - 1; j++) {
- switch (moves(curX, curY, M, N, matrix)) {
- case 4:
- System.out.println("case 4:");
- curY--;
- if (matrix[i][j]-- >= 0) {
- matrix[i][j]--;
- coins++;
- }
- break;
- case 3:
- System.out.println("case 3:");
- curY++;
- if (matrix[i][j]-- >= 0) {
- matrix[i][j]--;
- coins++;
- }
- coins++;
- break;
- case 2:
- System.out.println("case 2:");
- curX--;
- if (matrix[i][j]-- >= 0) {
- matrix[i][j]--;
- coins++;
- }
- break;
- case 1:
- System.out.println("case 1:");
- curX++;
- if (matrix[i][j]-- >= 0) {
- matrix[i][j]--;
- coins++;
- }
- break;
- default:
- System.out.println("case default:");
- System.out.println(coins);
- }
- }
- }
- }
- private static int mostCoins(int i, int j, int[][] arr) {
- System.out.println("We used most Coins");
- return Math.max(arr[i][j - 1], Math.max(arr[i][j + 1], Math.max(arr[i - 1][j], arr[i + 1][j])));
- }
- private static int moves(int i, int j, int rows, int cols, int[][] arr) {
- System.out.println("We made a move");
- if (canGoLeft(j) && arr[i][j - 1] == mostCoins(rows, cols, arr)) {
- System.out.println("We made a move on left");
- return 4;
- } else if (canGoRight(j, cols) && arr[i][j + 1] == mostCoins(rows, cols, arr)) {
- System.out.println("We made a move on right");
- return 3;
- } else if (canGoUP(i) && arr[i - 1][j] == mostCoins(rows, cols, arr)) {
- System.out.println("We made a move on up");
- return 2;
- } else if (canGoDown(i, rows) && arr[i + 1][j] == mostCoins(rows, cols, arr)) {
- System.out.println("We made a move on down");
- return 1;
- } else {
- System.out.println("We made a move out");
- return -1;
- }
- }
- private static boolean canGoLeft(int y) {
- System.out.println("we can go left now:");
- return y - 1 >= 0;
- }
- private static boolean canGoRight(int y, int cols) {
- System.out.println("we can go right now:");
- return y + 1 < cols;
- }
- private static boolean canGoUP(int x) {
- System.out.println("we can go up now:");
- return x - 1 >= 0;
- }
- private static boolean canGoDown(int x, int rows) {
- System.out.println("we can go down now:");
- return x + 1 > rows;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement