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 McDuck {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //N and M
- String line = br.readLine();
- String[] dimensions = line.split(" ");
- int N = Integer.parseInt(dimensions[0]);
- int M = Integer.parseInt(dimensions[1]);
- // matrix NxM
- int[][] matrix = new int[N][M];
- //Where is Scrooge McDuck now
- int curX = -1;
- int curY = -1;
- //Filling the matrix with M-стойности на всеки от N-те реда
- 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]);
- //Finding McDuck here
- if (matrix[i][j] == 0) {
- curX = i;
- curY = j;
- }
- }
- }
- int coins = 0;
- while (bestMove(curX, curY, N, M, matrix) != -1 && !(curX - 1 <= 0 && curX + 1 >= 0 && curY - 1 <= 0 && curY + 1 >= 0)) {
- switch (bestMove(curX, curY, N, M, matrix)) {
- case 4:
- System.out.println("case 4:");
- if (matrix[curX][curY]-- >= 0) {
- matrix[curX][curY]--;
- curY--;
- coins++;
- }
- curY--;
- break;
- case 3:
- System.out.println("case 3:");
- if (matrix[curX][curY]-- >= 0) {
- matrix[curX][curY]--;
- curY++;
- coins++;
- }
- curY++;
- break;
- case 2:
- System.out.println("case 2:");
- if (matrix[curX][curY]-- >= 0) {
- matrix[curX][curY]--;
- curX--;
- coins++;
- }
- curX--;
- break;
- case 1:
- System.out.println("case 1:");
- if (matrix[curX][curY]-- >= 0) {
- matrix[curX][curY]--;
- curX++;
- coins++;
- }
- curX++;
- break;
- default:
- System.out.println("case default:");
- System.out.println(coins);
- }
- }
- System.out.println(coins);
- }
- private static int bestMove(int curI, int curJ, int rows, int cols, int[][] matrix) {
- int direction = -1;
- System.out.println("We made a move");
- if (curJ - 1 >= 0 && matrix[curI][curJ - 1] == mostCoins(curI, curJ, matrix)) {
- System.out.println("We made a move on left");
- direction = 4;
- } else if (curJ + 1 < cols && matrix[curI][curJ + 1] == mostCoins(curI, curJ, matrix)) {
- System.out.println("We made a move on right");
- direction = 3;
- } else if (curI - 1 >= 0 && matrix[curI - 1][curJ] == mostCoins(curI, curJ, matrix)) {
- System.out.println("We made a move on up");
- direction = 2;
- } else if (curI + 1 < rows && matrix[curI + 1][curJ] == mostCoins(curI, curJ, matrix)) {
- System.out.println("We made a move on down");
- direction = 1;
- } else {
- System.out.println("We made a move out");
- }
- return direction;
- }
- private static int mostCoins(int i, int j, int[][] matrix) {
- System.out.println("We used most Coins");
- return Math.max(matrix[i][j - 1], Math.max(matrix[i][j + 1], Math.max(matrix[i - 1][j], matrix[i + 1][j])));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement