Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _07_Collect_the_Coins2 {         // 100/100
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.  
  7.         char[][] matrix = new char[4][];
  8.         for (int i = 0; i < 4; i++) {
  9.             char[] currentRow = in.nextLine().toCharArray();
  10.             matrix[i] = new char[currentRow.length];
  11.             System.arraycopy(currentRow, 0, matrix[i], 0, currentRow.length);
  12.         }
  13.  
  14.         char[] moves = in.nextLine().toCharArray();
  15.         int currentRow = 0;
  16.         int currentCol = 0;
  17.  
  18.         int coins = 0;
  19.         int wallsHit = 0;
  20.         if (matrix[currentRow][currentCol] == '$')
  21.             coins++;
  22.  
  23.         for (char move : moves) {
  24.             int copyRow = currentRow;
  25.             int copyCol = currentCol;
  26.  
  27.             switch (move) {
  28.                 case '^':
  29.                     currentRow--;
  30.                     break;
  31.                 case '>':
  32.                     currentCol++;
  33.                     break;
  34.                 case 'V':
  35.                     currentRow++;
  36.                     break;
  37.                 case '<':
  38.                     currentCol--;
  39.                     break;
  40.             }
  41.  
  42.             if (!isInMatrix(currentRow, currentCol, matrix)) {
  43.                 currentRow = copyRow;
  44.                 currentCol = copyCol;
  45.                 wallsHit++;
  46.             } else {
  47.                 if (matrix[currentRow][currentCol] == '$')
  48.                     coins++;
  49.             }
  50.         }
  51.  
  52.         System.out.println("Coins = " + coins);
  53.         System.out.println("Walls = " + wallsHit);
  54.     }
  55.  
  56.     private static boolean isInMatrix(int currentRow, int currentCol, char[][] matrix) {
  57.         return currentRow >= 0 && currentCol >= 0 && currentRow < 4 && currentCol < matrix[currentRow].length;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement