Advertisement
Guest User

LadyBugs

a guest
Dec 9th, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Ladybugs
  5. {
  6. class Ladybug
  7. {
  8. static void Main(string[] args)
  9. {
  10. int size = int.Parse(Console.ReadLine());
  11. int[] field = new int[size];
  12. int[] indexes = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13.  
  14. for (int i = 0; i < field.Length; i++)
  15. {
  16. if (indexes.Contains(i))
  17. {
  18. field[i] = 1;
  19. }
  20. }
  21.  
  22. string[] command = Console.ReadLine().Split();
  23.  
  24. while (!command[0].Equals("end"))
  25. {
  26. int ladybugIndex = int.Parse(command[0]);
  27. string direction = command[1];
  28. int flightLength = int.Parse(command[2]);
  29.  
  30. if (ladybugIndex < 0 || ladybugIndex >= field.Length)
  31. {
  32. command = Console.ReadLine().Split();
  33. continue;
  34. }
  35. if (field[ladybugIndex] == 0)
  36. {
  37. command = Console.ReadLine().Split();
  38. continue;
  39. }
  40. if (direction.Equals("right"))
  41. {
  42. if (ladybugIndex + flightLength >= field.Length)
  43. {
  44. field[ladybugIndex] = 0;
  45. command = Console.ReadLine().Split();
  46. continue;
  47. }
  48. if (flightLength > 0)
  49. {
  50. FlyRight(ladybugIndex, flightLength, field);
  51. }
  52. else if (flightLength < 0)
  53. {
  54. flightLength = Math.Abs(flightLength);
  55.  
  56. FlyLeft(ladybugIndex, flightLength, field);
  57. }
  58. }
  59. else if (direction.Equals("left"))
  60. {
  61. if (ladybugIndex - flightLength < 0)
  62. {
  63. field[ladybugIndex] = 0;
  64. command = Console.ReadLine().Split();
  65. continue;
  66. }
  67. if (flightLength > 0)
  68. {
  69. FlyLeft(ladybugIndex, flightLength, field);
  70. }
  71. else if (flightLength < 0)
  72. {
  73. flightLength = Math.Abs(flightLength);
  74.  
  75. FlyRight(ladybugIndex, flightLength, field);
  76. }
  77. }
  78.  
  79. command = Console.ReadLine().Split();
  80. }
  81.  
  82. Console.WriteLine(string.Join(" ", field));
  83. }
  84.  
  85. public static void FlyRight(int startIndex, int flyLength, int[] field)
  86. {
  87. field[startIndex] = 0;
  88.  
  89. for (int i = startIndex + flyLength; i < field.Length; i += flyLength)
  90. {
  91. if (field[i] == 1)
  92. {
  93. continue;
  94. }
  95. else
  96. {
  97. field[i] = 1;
  98. return;
  99. }
  100. }
  101. }
  102.  
  103. public static void FlyLeft(int startIndex, int flyLength, int[] field)
  104. {
  105. field[startIndex] = 0;
  106.  
  107. for (int i = startIndex - flyLength; i >= 0; i -= flyLength)
  108. {
  109. if (field[i] == 1)
  110. {
  111. continue;
  112. }
  113. else
  114. {
  115. field[i] = 1;
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement