Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Help {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- char[][] field = new char[n][n];
- int beeRow = 0, beeCol = 0;
- // Reading the matrix
- for (int i = 0; i < n; i++) {
- String line = scanner.nextLine();
- for (int j = 0; j < n; j++) {
- field[i][j] = line.charAt(j);
- if (field[i][j] == 'B') {
- beeRow = i;
- beeCol = j;
- }
- }
- }
- int energy = 15;
- int nectarCollected = 0;
- boolean energyRestored = false;
- boolean hiveReached = false;
- // Movement commands
- while (scanner.hasNextLine()) {
- String command = scanner.nextLine();
- int[] newCoords = move(command, beeRow, beeCol, n);
- int newRow = newCoords[0];
- int newCol = newCoords[1];
- // Remove bee from current position
- field[beeRow][beeCol] = '-';
- // Check if bee collects nectar
- if (Character.isDigit(field[newRow][newCol])) {
- int nectar = Character.getNumericValue(field[newRow][newCol]);
- nectarCollected += nectar;
- field[newRow][newCol] = '-';
- }
- // Update bee's position
- beeRow = newRow;
- beeCol = newCol;
- // Decrease energy with each move
- energy--;
- // Check if bee reaches the hive
- if (field[beeRow][beeCol] == 'H') {
- hiveReached = true;
- break;
- }
- // Check if bee runs out of energy
- if (energy == 0) {
- if (nectarCollected >= 30 && !energyRestored) {
- int difference = nectarCollected - 30;
- energy += difference;
- nectarCollected = 30;
- energyRestored = true;
- } else {
- break;
- }
- }
- // Place bee at new position
- field[beeRow][beeCol] = 'B';
- }
- // Output results
- if (hiveReached && nectarCollected >= 30) {
- System.out.printf("Great job, Beesy! The hive is full. Energy left: %d%n", energy);
- } else if (hiveReached) {
- System.out.println("Beesy did not manage to collect enough nectar.");
- } else {
- System.out.println("This is the end! Beesy ran out of energy.");
- }
- // Print final state of the matrix
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (i == beeRow && j == beeCol) {
- System.out.print('B');
- } else {
- System.out.print(field[i][j]);
- }
- }
- System.out.println();
- }
- }
- // Method to calculate new coordinates after movement
- private static int[] move(String command, int currentRow, int currentCol, int size) {
- int[] newCoords = new int[2];
- switch (command) {
- case "up":
- newCoords[0] = (currentRow - 1 + size) % size;
- newCoords[1] = currentCol;
- break;
- case "down":
- newCoords[0] = (currentRow + 1) % size;
- newCoords[1] = currentCol;
- break;
- case "left":
- newCoords[0] = currentRow;
- newCoords[1] = (currentCol - 1 + size) % size;
- break;
- case "right":
- newCoords[0] = currentRow;
- newCoords[1] = (currentCol + 1) % size;
- break;
- }
- return newCoords;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement