Advertisement
Guest User

Untitled

a guest
Nov 27th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ladybugs {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int fieldSize = Integer.parseInt(scanner.nextLine());
  9.  
  10. int[] field = new int[fieldSize];
  11.  
  12. int[] bugs = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
  13.  
  14. for (int ifBug : bugs) {
  15. field[ifBug] = 1;
  16. }
  17.  
  18. outter:
  19. while (true) {
  20. String input = scanner.nextLine();
  21.  
  22. if ("end".equals(input)) {
  23. break;
  24. }
  25.  
  26. String[] commands = input.split(" ");
  27.  
  28. int position = Integer.parseInt(commands[0]);
  29. String direction = commands[1];
  30. int flyLenght = Integer.parseInt(commands[2]);
  31. // If you are given ladybug index that is outside the field
  32. if (position < 0 || position >= field.length) {
  33. continue;
  34. }
  35. //If you are given ladybug index that does not have ladybug there
  36. if (field[position] == 0) {
  37. continue;
  38. //empty the index after it has flew away
  39. } else {
  40. field[position] = 0;
  41. }
  42.  
  43. if (direction.equals("right")) {
  44. //if it lands somewhere outside of the field
  45. if (position + flyLenght >= field.length) {
  46. continue;
  47. }
  48.  
  49. //if landing index is free
  50. if (field[position + flyLenght] == 0) {
  51. field[position + flyLenght] = 1;
  52. //if landing index is occupied
  53. } else {
  54. int landingIndex = position + flyLenght;
  55. while (field[landingIndex] == 1) {
  56. landingIndex += flyLenght;
  57. if (landingIndex >= field.length) {
  58. continue outter;
  59. }
  60. }
  61.  
  62. field[landingIndex] = 1;
  63. }
  64. } else if (direction.equals("left")) {
  65. //if it lands somewhere outside of the field
  66. if (position - flyLenght < 0) {
  67. continue;
  68. }
  69.  
  70. //if landing index is free
  71. if (field[position - flyLenght] == 0) {
  72. field[position - flyLenght] = 1;
  73. //if landing index is occupied
  74. } else {
  75. int landingIndex = position - flyLenght;
  76. while (field[landingIndex] == 1) {
  77. landingIndex -= flyLenght;
  78. if (landingIndex < 0) {
  79. continue outter;
  80. }
  81. }
  82.  
  83. field[landingIndex] = 1;
  84. }
  85. }
  86. }
  87.  
  88. for (int print : field) {
  89. System.out.print(print + " ");
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement