Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 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 ScroogeMcDuck {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.         String line = br.readLine();
  11.         String[] dimensions = line.split(" ");
  12.         int N = Integer.parseInt(dimensions[0]);
  13.         int M = Integer.parseInt(dimensions[1]);
  14.         int[][] matrix = new int[N][M];
  15.  
  16.         int curX = 0;
  17.         int curY = 0;
  18.  
  19.         int coins = 0;
  20.         for (int i = 0; i < N; i++) {
  21.             String lines = br.readLine();
  22.             String[] nLines = lines.split(" ");
  23.             for (int j = 0; j < M; j++) {
  24.                 matrix[i][j] = Integer.parseInt(nLines[j]);
  25.                 if (matrix[i][j] == 0) {
  26.                     curX = i;
  27.                     curY = j;
  28.                     System.out.println("filling the current possition: " + i + " " + j);
  29.                 }
  30.             }
  31.         }
  32.         System.out.println("printing matrix now: ");
  33.         for (int i = 0; i < N; i++) {
  34.             for (int j = 0; j < M; j++) {
  35.                 System.out.print(matrix[i][j] + " ");
  36.             }
  37.             System.out.println();
  38.         }
  39.         for (int i = 1; i < M - 1; i++) {
  40.             for (int j = 1; j < N - 1; j++) {
  41.                 switch (moves(curX, curY, M, N, matrix)) {
  42.                     case 4:
  43.                         System.out.println("case 4:");
  44.                         curY--;
  45.                         if (matrix[i][j]-- >= 0) {
  46.                             matrix[i][j]--;
  47.                             coins++;
  48.                         }
  49.                         break;
  50.                     case 3:
  51.                         System.out.println("case 3:");
  52.                         curY++;
  53.                         if (matrix[i][j]-- >= 0) {
  54.                             matrix[i][j]--;
  55.                             coins++;
  56.                         }
  57.                         coins++;
  58.                         break;
  59.                     case 2:
  60.                         System.out.println("case 2:");
  61.                         curX--;
  62.                         if (matrix[i][j]-- >= 0) {
  63.                             matrix[i][j]--;
  64.                             coins++;
  65.                         }
  66.                         break;
  67.                     case 1:
  68.                         System.out.println("case 1:");
  69.                         curX++;
  70.                         if (matrix[i][j]-- >= 0) {
  71.                             matrix[i][j]--;
  72.                             coins++;
  73.                         }
  74.                         break;
  75.                     default:
  76.                         System.out.println("case default:");
  77.                         System.out.println(coins);
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.  
  84.     private static int mostCoins(int i, int j, int[][] arr) {
  85.         System.out.println("We used most Coins");
  86.         return Math.max(arr[i][j - 1], Math.max(arr[i][j + 1], Math.max(arr[i - 1][j], arr[i + 1][j])));
  87.     }
  88.  
  89.     private static int moves(int i, int j, int rows, int cols, int[][] arr) {
  90.         System.out.println("We made a move");
  91.         if (canGoLeft(j) && arr[i][j - 1] == mostCoins(rows, cols, arr)) {
  92.             System.out.println("We made a move on left");
  93.             return 4;
  94.         } else if (canGoRight(j, cols) && arr[i][j + 1] == mostCoins(rows, cols, arr)) {
  95.             System.out.println("We made a move on right");
  96.             return 3;
  97.         } else if (canGoUP(i) && arr[i - 1][j] == mostCoins(rows, cols, arr)) {
  98.             System.out.println("We made a move on up");
  99.             return 2;
  100.         } else if (canGoDown(i, rows) && arr[i + 1][j] == mostCoins(rows, cols, arr)) {
  101.             System.out.println("We made a move on down");
  102.             return 1;
  103.         } else {
  104.             System.out.println("We made a move out");
  105.             return -1;
  106.         }
  107.     }
  108.  
  109.     private static boolean canGoLeft(int y) {
  110.         System.out.println("we can go left now:");
  111.         return y - 1 >= 0;
  112.     }
  113.  
  114.     private static boolean canGoRight(int y, int cols) {
  115.         System.out.println("we can go right now:");
  116.         return y + 1 < cols;
  117.     }
  118.  
  119.     private static boolean canGoUP(int x) {
  120.         System.out.println("we can go up now:");
  121.         return x - 1 >= 0;
  122.     }
  123.  
  124.     private static boolean canGoDown(int x, int rows) {
  125.         System.out.println("we can go down now:");
  126.         return x + 1 > rows;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement