Guest User

Ladybug

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