Advertisement
Ligh7_of_H3av3n

Beesy

Jun 22nd, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Help {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         char[][] field = new char[n][n];
  12.         int beeRow = 0, beeCol = 0;
  13.  
  14.         // Reading the matrix
  15.         for (int i = 0; i < n; i++) {
  16.             String line = scanner.nextLine();
  17.             for (int j = 0; j < n; j++) {
  18.                 field[i][j] = line.charAt(j);
  19.                 if (field[i][j] == 'B') {
  20.                     beeRow = i;
  21.                     beeCol = j;
  22.                 }
  23.             }
  24.         }
  25.  
  26.         int energy = 15;
  27.         int nectarCollected = 0;
  28.         boolean energyRestored = false;
  29.         boolean hiveReached = false;
  30.  
  31.         // Movement commands
  32.         while (scanner.hasNextLine()) {
  33.             String command = scanner.nextLine();
  34.             int[] newCoords = move(command, beeRow, beeCol, n);
  35.             int newRow = newCoords[0];
  36.             int newCol = newCoords[1];
  37.  
  38.             // Remove bee from current position
  39.             field[beeRow][beeCol] = '-';
  40.  
  41.             // Check if bee collects nectar
  42.             if (Character.isDigit(field[newRow][newCol])) {
  43.                 int nectar = Character.getNumericValue(field[newRow][newCol]);
  44.                 nectarCollected += nectar;
  45.                 field[newRow][newCol] = '-';
  46.             }
  47.  
  48.             // Update bee's position
  49.             beeRow = newRow;
  50.             beeCol = newCol;
  51.  
  52.             // Decrease energy with each move
  53.             energy--;
  54.  
  55.             // Check if bee reaches the hive
  56.             if (field[beeRow][beeCol] == 'H') {
  57.                 hiveReached = true;
  58.                 break;
  59.             }
  60.  
  61.             // Check if bee runs out of energy
  62.             if (energy == 0) {
  63.                 if (nectarCollected >= 30 && !energyRestored) {
  64.                     int difference = nectarCollected - 30;
  65.                     energy += difference;
  66.                     nectarCollected = 30;
  67.                     energyRestored = true;
  68.                 } else {
  69.                     break;
  70.                 }
  71.             }
  72.  
  73.             // Place bee at new position
  74.             field[beeRow][beeCol] = 'B';
  75.         }
  76.  
  77.         // Output results
  78.         if (hiveReached && nectarCollected >= 30) {
  79.             System.out.printf("Great job, Beesy! The hive is full. Energy left: %d%n", energy);
  80.         } else if (hiveReached) {
  81.             System.out.println("Beesy did not manage to collect enough nectar.");
  82.         } else {
  83.             System.out.println("This is the end! Beesy ran out of energy.");
  84.         }
  85.  
  86.         // Print final state of the matrix
  87.         for (int i = 0; i < n; i++) {
  88.             for (int j = 0; j < n; j++) {
  89.                 if (i == beeRow && j == beeCol) {
  90.                     System.out.print('B');
  91.                 } else {
  92.                     System.out.print(field[i][j]);
  93.                 }
  94.             }
  95.             System.out.println();
  96.         }
  97.     }
  98.  
  99.     // Method to calculate new coordinates after movement
  100.     private static int[] move(String command, int currentRow, int currentCol, int size) {
  101.         int[] newCoords = new int[2];
  102.         switch (command) {
  103.             case "up":
  104.                 newCoords[0] = (currentRow - 1 + size) % size;
  105.                 newCoords[1] = currentCol;
  106.                 break;
  107.             case "down":
  108.                 newCoords[0] = (currentRow + 1) % size;
  109.                 newCoords[1] = currentCol;
  110.                 break;
  111.             case "left":
  112.                 newCoords[0] = currentRow;
  113.                 newCoords[1] = (currentCol - 1 + size) % size;
  114.                 break;
  115.             case "right":
  116.                 newCoords[0] = currentRow;
  117.                 newCoords[1] = (currentCol + 1) % size;
  118.                 break;
  119.         }
  120.         return newCoords;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement