Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class LootBox {
- public static void main(String[] args) throws IOException {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- String initialString = bf.readLine();
- StringBuilder stringBuilder = new StringBuilder(initialString);
- int n = Integer.parseInt(bf.readLine());
- String[][] matrix = new String[n][n];
- int playerRows = 0;
- int playerCows = 0;
- for (int i = 0; i < matrix.length; i++) {
- matrix[i] = bf.readLine().split("");
- for (int j = 0; j < matrix[i].length; j++) {
- if (matrix[i][j].equals("P")) {
- playerRows = i;
- playerCows = i;
- }
- }
- }
- matrix[playerRows][playerCows] = "-";
- String command;
- while (!(command = bf.readLine()).equals("end")) {
- switch (command) {
- case "up":
- playerRows--;
- if (isValid(matrix, playerRows, playerCows)) {
- String current = "";
- if (!matrix[playerRows][playerCows].equals("-")) {
- current+=matrix[playerRows][playerCows];
- stringBuilder.append(current);
- matrix[playerRows][playerCows] = "-";
- }
- } else {
- playerCows++;
- if(stringBuilder.length()>0) {
- stringBuilder.deleteCharAt(stringBuilder.length()-1);
- }
- }
- break;
- case "down":
- playerRows++;
- if (isValid(matrix, playerRows, playerCows)) {
- String current = "";
- if (!matrix[playerRows][playerCows].equals("-")) {
- current+=matrix[playerRows][playerCows];
- stringBuilder.append(current);
- matrix[playerRows][playerCows] = "-";
- }
- } else {
- playerCows++;
- if(stringBuilder.length()>0) {
- stringBuilder.deleteCharAt(stringBuilder.length()-1);
- }
- }
- break;
- case "left":
- playerCows--;
- if (isValid(matrix, playerRows, playerCows)) {
- String current = "";
- if (!matrix[playerRows][playerCows].equals("-")) {
- current+=matrix[playerRows][playerCows];
- stringBuilder.append(current);
- matrix[playerRows][playerCows] = "-";
- }
- } else {
- playerCows++;
- if(stringBuilder.length()>0) {
- stringBuilder.deleteCharAt(stringBuilder.length()-1);
- }
- }
- break;
- case "right":
- playerCows++;
- if (isValid(matrix, playerRows, playerCows)) {
- String current = "";
- if (!matrix[playerRows][playerCows].equals("-")) {
- current+=matrix[playerRows][playerCows];
- stringBuilder.append(current);
- matrix[playerRows][playerCows] = "-";
- }
- } else {
- playerCows++;
- if(stringBuilder.length()>0) {
- stringBuilder.deleteCharAt(stringBuilder.length()-1);
- }
- }
- break;
- }
- }
- matrix[playerRows][playerCows] = "P";
- System.out.println(stringBuilder);
- printMatrix(matrix);
- }
- public static void printMatrix(String[][] mx) {
- for (int i = 0; i < mx.length; i++) {
- for (int j = 0; j < mx[i].length; j++) {
- System.out.print(mx[i][j]);
- }
- System.out.println();
- }
- }
- public static boolean isValid(String[][] matrix, int row, int col) {
- return row >= 0 && row < matrix.length && col >= 0 && col < matrix.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement