Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int energy = Integer.parseInt(scanner.nextLine());
- int rows = Integer.parseInt(scanner.nextLine());
- char[][] field = new char[rows][];
- int parisRow = 0;
- int parisCol = 0;
- for (int i = 0; i < rows; i++) {
- String line = scanner.nextLine();
- field[i] = line.toCharArray();
- if (line.contains("P")) {
- parisRow = i;
- parisCol = line.indexOf("P");
- }
- }
- boolean isAbducted = false;
- while (energy > 0 && !isAbducted) {
- energy--;
- String direction = scanner.next();
- int row = scanner.nextInt();
- int col = scanner.nextInt();
- field[row][col] = 'S';
- field[parisRow][parisCol] = '-';
- if (direction.equals("up") && canMove(parisRow - 1, parisCol, field)) {
- parisRow--;
- if (field[parisRow][parisCol] == 'S') {
- energy -= 2;
- } else if (field[parisRow][parisCol] == 'H') {
- isAbducted = true;
- }
- } else if (direction.equals("down") && canMove(parisRow + 1, parisCol, field)) {
- parisRow++;
- if (field[parisRow][parisCol] == 'S') {
- energy -= 2;
- } else if (field[parisRow][parisCol] == 'H') {
- isAbducted = true;
- }
- } else if (direction.equals("left") && canMove(parisRow, parisCol - 1, field)) {
- parisCol--;
- if (field[parisRow][parisCol] == 'S') {
- energy -= 2;
- } else if (field[parisRow][parisCol] == 'H') {
- isAbducted = true;
- }
- } else if (direction.equals("right") && canMove(parisRow, parisCol + 1, field)) {
- parisCol++;
- if (field[parisRow][parisCol] == 'S') {
- energy -= 2;
- } else if (field[parisRow][parisCol] == 'H') {
- isAbducted = true;
- }
- }
- field[parisRow][parisCol] = 'P';
- }
- if (isAbducted) {
- field[parisRow][parisCol] = '-';
- System.out.println("Paris has successfully abducted Helen! Energy left: " + energy);
- } else {
- field[parisRow][parisCol] = 'X';
- System.out.printf("Paris died at %d;%d.\n", parisRow, parisCol);
- }
- printField(field);
- }
- private static void printField(char[][] field) {
- for (int r = 0; r < field.length; r++) {
- for (int c = 0; c < field[r].length; c++) {
- System.out.print(field[r][c]);
- }
- System.out.println();
- }
- }
- private static boolean canMove(int newRow, int newCol, char[][] field) {
- return newRow >= 0
- && newRow < field.length
- && newCol >= 0
- && newCol < field[newCol].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement