Advertisement
CR7CR7

LB

Oct 12th, 2022
2,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.94 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class LadyBugs {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int[] field = new int[Integer.parseInt(scanner.nextLine())];
  8.         int[] indexes = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
  9.         for (int i = 0; i < indexes.length; i++) {
  10.             if (indexes[i] >= 0 && indexes[i] < field.length) {
  11.                 field[indexes[i]] = 1;
  12.             }
  13.         }
  14.         String command;
  15.         while (!(command = scanner.nextLine()).equals("end")) {
  16.             String[] input = command.split(" ");
  17.             int index = Integer.parseInt(input[0]);
  18.             String direction = input[1];
  19.             int flight = Integer.parseInt(input[2]);
  20.  
  21.             if (index < 0 || index >= field.length || field[index] == 0) {
  22.                 continue;
  23.             }
  24.             field[index] = 0;
  25.             int currentFlight = flight;
  26.             if (direction.equals("right")) {
  27.                 while (index + currentFlight < field.length && index + currentFlight >= 0) {
  28.                     if (field[index + currentFlight] == 0) {
  29.                         field[index + currentFlight] = 1;
  30.                         break;
  31.                     } else {
  32.                         currentFlight += flight;
  33.                     }
  34.                 }
  35.             } else if (direction.equals("left")) {
  36.                 while (index - currentFlight >= 0 && index - currentFlight < field.length) {
  37.                     if (field[index - currentFlight] == 0) {
  38.                         field[index - currentFlight] = 1;
  39.                         break;
  40.                     } else {
  41.                         currentFlight += flight;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         for (int cell : field) {
  47.             System.out.print(cell + " ");
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement