Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package com.company;
  2. import java.util.*;
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.     Scanner input = new Scanner(System.in);
  7.     int sizeOfField = Integer.parseInt(input.nextLine());
  8.     String ladybugIndexes = input.nextLine();
  9.     String commands;
  10.     int swap;
  11.     int [] arrayOfLadybugs = Arrays
  12.                             .stream(ladybugIndexes.split(" "))
  13.                             .mapToInt(Integer::parseInt)
  14.                             .toArray();
  15.     int [] field = new int[sizeOfField];
  16.  
  17.     for(int i=0; i<field.length; i++) {
  18.         for(int j=0; j<arrayOfLadybugs.length; j++) {
  19.             if(i == arrayOfLadybugs[j]) {
  20.                 field[i] = 1;
  21.                 break;
  22.             } else {
  23.                 field[i] = 0;
  24.             }
  25.         }
  26.     }
  27.  
  28.  
  29.     while(true) {
  30.         commands = input.nextLine();
  31.         if(commands.equals("end")) break;
  32.         else{
  33.             String[] commandsArray= commands.split(" ");
  34.             int ladybugFromCommand = Integer.parseInt(commandsArray[0]);
  35.             int movement = Integer.parseInt(commandsArray[2]);
  36.             for(int i=0; i<field.length; i++) {
  37.                 if(ladybugFromCommand == i) {
  38.                     if(commandsArray[1].equals("right")) {
  39.                         if(i+movement < field.length) {
  40.                             if(field[i+movement] == 0) {
  41.                                 field[i + movement] = 1;
  42.                                 field[i] = 0;
  43.                             }
  44.                             else {
  45.                                 for(int l=i; l<field.length; l++)
  46.                                 {
  47.                                     if(field[l] == 0) {
  48.                                         field[l] = 1;
  49.                                         field[i] = 0;
  50.                                         break;
  51.                                     }
  52.                                     else field[i] = 0;
  53.                                 }
  54.                             }
  55.                         }
  56.                         else field[i]=0;
  57.                     }
  58.                     else if(commandsArray[1].equals("left")) {
  59.                         if(i-movement >= 0) {
  60.                             if(field[i-movement] == 0) {
  61.                                 field[i - movement] = 1;
  62.                                 field[i] = 0;
  63.                             }
  64.                             else {
  65.                                 for(int l=field.length-1; l>=i; l--)
  66.                                 {
  67.                                     if(field[l] == 0) {
  68.                                         field[l] = 1;
  69.                                         field[i] = 0;
  70.                                         break;
  71.                                     }
  72.                                     else field[i]=0;
  73.                             }
  74.                         }
  75.                     }
  76.                         else field[i]=0;
  77.                 }
  78.             }
  79.         }
  80.  
  81.     }
  82.  
  83.     }
  84.         for(int i=0; i<field.length; i++)
  85.         {
  86.             System.out.print(field[i]+ " ");
  87.         }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement