Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Camera_View
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. int sizeOfField = int.Parse(Console.ReadLine().Trim());
  15. long[] indexesOfLadybugs = Regex.Split(Console.ReadLine(), @"\s+").Select(long.Parse).ToArray();
  16. int[] fieldWithLadybugs = PlaceLadybugs(indexesOfLadybugs, sizeOfField);
  17. string input = Console.ReadLine();
  18. while (input != "end")
  19. {
  20. fieldWithLadybugs = MoveLadybugs(fieldWithLadybugs, input);
  21. input = Console.ReadLine();
  22. }
  23. Console.WriteLine(string.Join(" ", fieldWithLadybugs));
  24. }
  25.  
  26. private static int[] MoveLadybugs(int[] fieldWithLadybugs, string input)
  27. {
  28. string[] commands = Regex.Split(input, @"\s+");
  29. int indexOfLadybug = int.Parse(commands[0]);
  30. if (indexOfLadybug < 0 || indexOfLadybug >= fieldWithLadybugs.Length)
  31. {
  32. return fieldWithLadybugs;
  33. }
  34. else if (fieldWithLadybugs[indexOfLadybug] == 0)
  35. {
  36. return fieldWithLadybugs;
  37. }
  38. string direction = commands[1];
  39. long flightDistance = long.Parse(commands[2]);
  40. if (direction == "right")
  41. {
  42. long indexToLand = indexOfLadybug + flightDistance;
  43. fieldWithLadybugs[indexOfLadybug] = 0;
  44. if (indexToLand >= fieldWithLadybugs.Length || indexToLand < 0)
  45. {
  46. return fieldWithLadybugs;
  47. }
  48. else if (indexToLand >= 0 && indexToLand < fieldWithLadybugs.Length)
  49. {
  50. fieldWithLadybugs = MoveCurrentLadybug(indexToLand, fieldWithLadybugs, indexOfLadybug);
  51. return fieldWithLadybugs;
  52. }
  53.  
  54. }
  55. else if (direction == "left")
  56. {
  57. long indexToLand = indexOfLadybug - flightDistance;
  58. fieldWithLadybugs[indexOfLadybug] = 0;
  59. if (indexToLand < 0 || indexToLand >= fieldWithLadybugs.Length)
  60. {
  61. return fieldWithLadybugs;
  62. }
  63. else if (indexToLand >= 0 && indexToLand < fieldWithLadybugs.Length)
  64. {
  65. fieldWithLadybugs = MoveCurrentLadybug(indexToLand, fieldWithLadybugs, indexOfLadybug);
  66. return fieldWithLadybugs;
  67. }
  68. }
  69. return fieldWithLadybugs;
  70. }
  71.  
  72. private static int[] MoveCurrentLadybug(long indexToLand, int[] fieldWithLadybugs, int indexOfLadybug)
  73. {
  74. if (fieldWithLadybugs[indexToLand] == 0)
  75. {
  76. fieldWithLadybugs[indexToLand] = 1;
  77. return fieldWithLadybugs;
  78. }
  79. else if (fieldWithLadybugs[indexToLand] != 0)
  80. {
  81. if (indexToLand > indexOfLadybug)
  82. {
  83. do
  84. {
  85. indexToLand++;
  86. if (indexToLand >= 0 && indexToLand < fieldWithLadybugs.Length)
  87. {
  88. if (fieldWithLadybugs[indexToLand] == 0)
  89. {
  90. fieldWithLadybugs[indexToLand] = 1;
  91. return fieldWithLadybugs;
  92. }
  93. }
  94. else
  95. {
  96. return fieldWithLadybugs;
  97. }
  98. } while (true);
  99. }
  100. else if (indexToLand < indexOfLadybug)
  101. {
  102. do
  103. {
  104. indexToLand--;
  105. if (indexToLand >= 0 && indexToLand < fieldWithLadybugs.Length)
  106. {
  107. if (fieldWithLadybugs[indexToLand] == 0)
  108. {
  109. fieldWithLadybugs[indexToLand] = 1;
  110. return fieldWithLadybugs;
  111. }
  112. }
  113. else
  114. {
  115. return fieldWithLadybugs;
  116. }
  117. } while (true);
  118. }
  119. }
  120. return fieldWithLadybugs;
  121. }
  122.  
  123. private static int[] PlaceLadybugs(long[] indexesOfLadybugs, int sizeOfField)
  124. {
  125. int[] arr = new int[sizeOfField];
  126. for (int i = 0; i < indexesOfLadybugs.Length; i++)
  127. {
  128. if (indexesOfLadybugs[i] >= 0 && indexesOfLadybugs[i] < arr.Length)
  129. {
  130. arr[indexesOfLadybugs[i]] = 1;
  131. }
  132. }
  133. return arr;
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement