Advertisement
Guest User

Untitled

a guest
Feb 10th, 2019
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class kur {
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         int sizeField = Integer.parseInt(sc.nextLine());
  10.         int[] field = new int[sizeField];
  11.         int[] positions = Arrays.stream(sc.nextLine().split(" "))
  12.                 .mapToInt(Integer::parseInt).toArray();
  13.        
  14.         for (int i = 0; i < positions.length; i++) {
  15.  
  16.             if (positions[i] < field.length && positions[i] >= 0) {
  17.                 field[positions[i]] = 1;
  18.             }
  19.         }
  20.  
  21.         String input = sc.nextLine();
  22.         String[] arrayedInput = input.split(" ");
  23.  
  24.         while (!"end".equals(input)) {
  25.  
  26.             int indexLadyBug = Integer.parseInt(arrayedInput[0]);
  27.             int flyDestination = Integer.parseInt(arrayedInput[2]);
  28.             String direction = arrayedInput[1];
  29.  
  30.             if (indexLadyBug >=0 && indexLadyBug < field.length && flyDestination != 0) {
  31.  
  32.                 switch (direction) {
  33.                     case "right":
  34.                         int newPosition = indexLadyBug + flyDestination;
  35.  
  36.                         if(newPosition < field.length && newPosition >= 0) {
  37.                             if (field[indexLadyBug] == 1 && field[newPosition] == 0) {
  38.                                 field[indexLadyBug] = 0;
  39.                                 field[newPosition] = 1;
  40.                             }else if (field[indexLadyBug] == 1 && field[flyDestination] == 1) {
  41.                                 for (int i = flyDestination * 2; true; i += flyDestination) {
  42.  
  43.                                     if (i > field.length - 1) {
  44.                                         field[indexLadyBug] = 0;
  45.                                         break;
  46.                                     }else if (field[i] == 0) {
  47.                                         field[i] = 1;
  48.                                         field[indexLadyBug] = 0;
  49.                                         break;
  50.                                     }
  51.  
  52.                                 }
  53.                             }
  54.                         }else {
  55.                             field[indexLadyBug] = 0;
  56.                         }
  57.                         break;
  58.  
  59.                     case "left":
  60.                         int newPositionLeft = indexLadyBug - flyDestination;
  61.  
  62.                         if(newPositionLeft >= 0 && newPositionLeft < field.length) {
  63.                             if(field[indexLadyBug] == 1 && field[newPositionLeft] == 0) {
  64.                                 field[indexLadyBug] = 0;
  65.                                 field[newPositionLeft] = 1;
  66.                             }else if(field[indexLadyBug] == 1 && field[flyDestination] == 1) {
  67.                                 for (int i = flyDestination * 2; true; i -= flyDestination) {
  68.  
  69.                                     if (i < 0) {
  70.                                         field[indexLadyBug] = 0;
  71.                                         break;
  72.                                     }else if(field[i] == 0) {
  73.                                         field[i] = 1;
  74.                                         field[indexLadyBug] = 0;
  75.                                         break;
  76.                                     }
  77.  
  78.                                 }
  79.                             }
  80.                         }else {
  81.                             field[indexLadyBug] = 0;
  82.                         }
  83.                         break;
  84.  
  85.                     default:
  86.                         break;
  87.                 }
  88.             }
  89.  
  90.  
  91.             input = sc.nextLine();
  92.             arrayedInput = input.split(" ");
  93.         }
  94.  
  95.         for (int bugs:field) {
  96.             System.out.print(bugs + " ");
  97.  
  98.         }
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement