Advertisement
ilianrusev

helen's abduction

Jun 22nd, 2019
201
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7.  
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int energy = Integer.parseInt(scanner.nextLine());
  11. int rows = Integer.parseInt(scanner.nextLine());
  12.  
  13. char[][] field = new char[rows][];
  14.  
  15. int parisRow = 0;
  16. int parisCol = 0;
  17.  
  18. for (int i = 0; i < rows; i++) {
  19. String line = scanner.nextLine();
  20. field[i] = line.toCharArray();
  21.  
  22. if (line.contains("P")) {
  23. parisRow = i;
  24. parisCol = line.indexOf("P");
  25. }
  26.  
  27. }
  28.  
  29. boolean isAbducted = false;
  30.  
  31. while (energy > 0 && !isAbducted) {
  32. energy--;
  33.  
  34. String direction = scanner.next();
  35. int row = scanner.nextInt();
  36. int col = scanner.nextInt();
  37.  
  38. field[row][col] = 'S';
  39. field[parisRow][parisCol] = '-';
  40. if (direction.equals("up") && canMove(parisRow - 1, parisCol, field)) {
  41. parisRow--;
  42. if (field[parisRow][parisCol] == 'S') {
  43. energy -= 2;
  44. } else if (field[parisRow][parisCol] == 'H') {
  45. isAbducted = true;
  46. }
  47.  
  48. } else if (direction.equals("down") && canMove(parisRow + 1, parisCol, field)) {
  49.  
  50. parisRow++;
  51. if (field[parisRow][parisCol] == 'S') {
  52. energy -= 2;
  53. } else if (field[parisRow][parisCol] == 'H') {
  54. isAbducted = true;
  55. }
  56. } else if (direction.equals("left") && canMove(parisRow, parisCol - 1, field)) {
  57. parisCol--;
  58.  
  59. if (field[parisRow][parisCol] == 'S') {
  60. energy -= 2;
  61. } else if (field[parisRow][parisCol] == 'H') {
  62. isAbducted = true;
  63. }
  64. } else if (direction.equals("right") && canMove(parisRow, parisCol + 1, field)) {
  65. parisCol++;
  66.  
  67. if (field[parisRow][parisCol] == 'S') {
  68. energy -= 2;
  69. } else if (field[parisRow][parisCol] == 'H') {
  70. isAbducted = true;
  71. }
  72.  
  73. }
  74. field[parisRow][parisCol] = 'P';
  75.  
  76. }
  77.  
  78. if (isAbducted) {
  79. field[parisRow][parisCol] = '-';
  80. System.out.println("Paris has successfully abducted Helen! Energy left: " + energy);
  81. } else {
  82. field[parisRow][parisCol] = 'X';
  83. System.out.printf("Paris died at %d;%d.\n", parisRow, parisCol);
  84. }
  85. printField(field);
  86. }
  87.  
  88. private static void printField(char[][] field) {
  89. for (int r = 0; r < field.length; r++) {
  90. for (int c = 0; c < field[r].length; c++) {
  91. System.out.print(field[r][c]);
  92. }
  93. System.out.println();
  94. }
  95. }
  96.  
  97. private static boolean canMove(int newRow, int newCol, char[][] field) {
  98. return newRow >= 0
  99. && newRow < field.length
  100. && newCol >= 0
  101. && newCol < field[newCol].length;
  102. }
  103. }
Advertisement
RAW Paste Data Copied
Advertisement