Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. package com.telerikacademy;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class McDuck {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.         //N and M
  11.         String line = br.readLine();
  12.         String[] dimensions = line.split(" ");
  13.         int N = Integer.parseInt(dimensions[0]);
  14.         int M = Integer.parseInt(dimensions[1]);
  15.  
  16.         // matrix NxM
  17.         int[][] matrix = new int[N][M];
  18.  
  19.         //Where is Scrooge McDuck now
  20.         int curX = -1;
  21.         int curY = -1;
  22.  
  23.         //Filling the matrix with M-стойности на всеки от N-те реда
  24.         for (int i = 0; i < N; i++) {
  25.             String lines = br.readLine();
  26.             String[] nLines = lines.split(" ");
  27.             for (int j = 0; j < M; j++) {
  28.                 matrix[i][j] = Integer.parseInt(nLines[j]);
  29.                 //Finding McDuck here
  30.                 if (matrix[i][j] == 0) {
  31.                     curX = i;
  32.                     curY = j;
  33.                 }
  34.             }
  35.         }
  36.         int coins = 0;
  37.         while (bestMove(curX, curY, N, M, matrix) != -1 && !(curX - 1 <= 0 && curX + 1 >= 0 && curY - 1 <= 0 && curY + 1 >= 0)) {
  38.                 switch (bestMove(curX, curY, N, M, matrix)) {
  39.                     case 4:
  40.                         System.out.println("case 4:");
  41.                         if (matrix[curX][curY]-- >= 0) {
  42.                             matrix[curX][curY]--;
  43.                             curY--;
  44.                             coins++;
  45.                         }
  46.                         curY--;
  47.                         break;
  48.                     case 3:
  49.                         System.out.println("case 3:");
  50.                         if (matrix[curX][curY]-- >= 0) {
  51.                             matrix[curX][curY]--;
  52.                             curY++;
  53.                             coins++;
  54.                         }
  55.                         curY++;
  56.                         break;
  57.                     case 2:
  58.                         System.out.println("case 2:");
  59.                         if (matrix[curX][curY]-- >= 0) {
  60.                             matrix[curX][curY]--;
  61.                             curX--;
  62.                             coins++;
  63.                         }
  64.                         curX--;
  65.                         break;
  66.                     case 1:
  67.                         System.out.println("case 1:");
  68.                         if (matrix[curX][curY]-- >= 0) {
  69.                             matrix[curX][curY]--;
  70.                             curX++;
  71.                             coins++;
  72.                         }
  73.                         curX++;
  74.                         break;
  75.                     default:
  76.                         System.out.println("case default:");
  77.                         System.out.println(coins);
  78.                 }
  79.             }
  80.         System.out.println(coins);
  81.  
  82.     }
  83.  
  84.     private static int bestMove(int curI, int curJ, int rows, int cols, int[][] matrix) {
  85.         int direction = -1;
  86.         System.out.println("We made a move");
  87.         if (curJ - 1 >= 0 && matrix[curI][curJ - 1] == mostCoins(curI, curJ, matrix)) {
  88.             System.out.println("We made a move on left");
  89.             direction = 4;
  90.         } else if (curJ + 1 < cols && matrix[curI][curJ + 1] == mostCoins(curI, curJ, matrix)) {
  91.             System.out.println("We made a move on right");
  92.             direction = 3;
  93.         } else if (curI - 1 >= 0 && matrix[curI - 1][curJ] == mostCoins(curI, curJ, matrix)) {
  94.             System.out.println("We made a move on up");
  95.             direction = 2;
  96.         } else if (curI + 1 < rows && matrix[curI + 1][curJ] == mostCoins(curI, curJ, matrix)) {
  97.             System.out.println("We made a move on down");
  98.             direction = 1;
  99.         } else {
  100.             System.out.println("We made a move out");
  101.         }
  102.         return direction;
  103.     }
  104.  
  105.     private static int mostCoins(int i, int j, int[][] matrix) {
  106.         System.out.println("We used most Coins");
  107.         return Math.max(matrix[i][j - 1], Math.max(matrix[i][j + 1], Math.max(matrix[i - 1][j], matrix[i + 1][j])));
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement