Advertisement
Ligh7_of_H3av3n

Beesy

Jul 2nd, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package Beesy;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Beesy {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         //•   On the first line, you are given the integer n – the size of the square matrix.
  11.         //•   The next n lines hold the values for every matrix row.
  12.         //•   After that, you will get a move command on each of the next lines.
  13.  
  14.         int n = Integer.parseInt(scanner.nextLine());
  15.         char[][] field = new char[n][n];
  16.         int beeRow = 0, beeCol = 0;
  17.  
  18.         //• The size of the square matrix (field) will be between [2…10].
  19.         //•   Only the letters 'B' and 'H' will be present in the matrix.
  20.         //•   The flowers with nectar are represented by single positive digits between [0…9].
  21.         //•   There will always be enough commands to finish the program.
  22.  
  23.         for (int i = 0; i < n; i++) {
  24.             String line = scanner.nextLine();
  25.             for (int j = 0; j < n; j++) {
  26.                 field[i][j] = line.charAt(j);
  27.                 if (field[i][j] == 'B') {
  28.                     beeRow = i;
  29.                     beeCol = j;
  30.                 }
  31.             }
  32.         }
  33.  
  34.         int energy = 15;
  35.         int nectarCollected = 0;
  36.         boolean energyRestored = false;
  37.         boolean hiveReached = false;
  38.  
  39.         while (true) {
  40.             String command = scanner.nextLine();
  41.  
  42.             //   Print the final state of the matrix, with the
  43.             //     last known position of the bee, marked with 'B'.
  44.             field[beeRow][beeCol] = '-';
  45.  
  46.             // •  After that, you will get a move command on each of the next lines.
  47.             switch (command) {
  48.                 case "up":
  49.                     beeRow = (beeRow - 1 + n) % n;
  50.                     break;
  51.                 case "down":
  52.                     beeRow = (beeRow + 1) % n;
  53.                     break;
  54.                 case "left":
  55.                     beeCol = (beeCol - 1 + n) % n;
  56.                     break;
  57.                 case "right":
  58.                     beeCol = (beeCol + 1) % n;
  59.                     break;
  60.             }
  61.  
  62.             energy--;
  63.  
  64.             // •  The flowers with nectar are represented by single positive digits between [0…9].
  65.             if (Character.isDigit(field[beeRow][beeCol])) {
  66.                 nectarCollected += Character.getNumericValue(field[beeRow][beeCol]);
  67.                 field[beeRow][beeCol] = '-';
  68.             }
  69.  
  70.             //•   Only the letters 'B' and 'H' will be present in the matrix.
  71.             if (field[beeRow][beeCol] == 'H') {
  72.                 hiveReached = true;
  73.                 break;
  74.             }
  75.  
  76.             // o    "This is the end! Beesy ran out of energy."
  77.             if (energy == 0) {
  78.                 if (nectarCollected >= 30 && !energyRestored) {
  79.                     energy = nectarCollected - 30;
  80.                     nectarCollected = 30;
  81.                     energyRestored = true;
  82.                 } else {
  83.                     break;
  84.                 }
  85.             }
  86.  
  87.  
  88.             field[beeRow][beeCol] = 'B';
  89.         }
  90.  
  91.         if (hiveReached && nectarCollected >= 30) {
  92.             System.out.println("Great job, Beesy! The hive is full. Energy left: " + energy);
  93.         } else if (hiveReached) {
  94.             System.out.println("Beesy did not manage to collect enough nectar.");
  95.         } else {
  96.             System.out.println("This is the end! Beesy ran out of energy.");
  97.         }
  98.  
  99.         // •  On the next lines.
  100.         //   Print the final state of the matrix, with the last
  101.         //  known position of the bee, marked with 'B'.
  102.  
  103.         field[beeRow][beeCol] = 'B';
  104.         for (int i = 0; i < n; i++) {
  105.             for (int j = 0; j < n; j++) {
  106.                 System.out.print(field[i][j]);
  107.             }
  108.             System.out.println();
  109.         }
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement