Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Beesy;
- import java.util.Scanner;
- public class Beesy {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //• On the first line, you are given the integer n – the size of the square matrix.
- //• The next n lines hold the values for every matrix row.
- //• After that, you will get a move command on each of the next lines.
- int n = Integer.parseInt(scanner.nextLine());
- char[][] field = new char[n][n];
- int beeRow = 0, beeCol = 0;
- //• The size of the square matrix (field) will be between [2…10].
- //• Only the letters 'B' and 'H' will be present in the matrix.
- //• The flowers with nectar are represented by single positive digits between [0…9].
- //• There will always be enough commands to finish the program.
- 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;
- while (true) {
- String command = scanner.nextLine();
- // Print the final state of the matrix, with the
- // last known position of the bee, marked with 'B'.
- field[beeRow][beeCol] = '-';
- // • After that, you will get a move command on each of the next lines.
- switch (command) {
- case "up":
- beeRow = (beeRow - 1 + n) % n;
- break;
- case "down":
- beeRow = (beeRow + 1) % n;
- break;
- case "left":
- beeCol = (beeCol - 1 + n) % n;
- break;
- case "right":
- beeCol = (beeCol + 1) % n;
- break;
- }
- energy--;
- // • The flowers with nectar are represented by single positive digits between [0…9].
- if (Character.isDigit(field[beeRow][beeCol])) {
- nectarCollected += Character.getNumericValue(field[beeRow][beeCol]);
- field[beeRow][beeCol] = '-';
- }
- //• Only the letters 'B' and 'H' will be present in the matrix.
- if (field[beeRow][beeCol] == 'H') {
- hiveReached = true;
- break;
- }
- // o "This is the end! Beesy ran out of energy."
- if (energy == 0) {
- if (nectarCollected >= 30 && !energyRestored) {
- energy = nectarCollected - 30;
- nectarCollected = 30;
- energyRestored = true;
- } else {
- break;
- }
- }
- field[beeRow][beeCol] = 'B';
- }
- if (hiveReached && nectarCollected >= 30) {
- System.out.println("Great job, Beesy! The hive is full. Energy left: " + 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.");
- }
- // • On the next lines.
- // Print the final state of the matrix, with the last
- // known position of the bee, marked with 'B'.
- field[beeRow][beeCol] = 'B';
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- System.out.print(field[i][j]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement