Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1
  9. {
  10. class Program
  11. {
  12. static StreamReader Reader = new StreamReader(@"stdin");
  13. static StreamWriter Writer = new StreamWriter(@"stdout");
  14. public enum Direction
  15. {
  16. Left,
  17. Right,
  18. Up,
  19. Down
  20. }
  21.  
  22. static Dictionary<string, Direction> dir = new Dictionary<string, Direction>()
  23. {
  24. {"L", Direction.Left},
  25. {"R", Direction.Right},
  26. {"U", Direction.Up},
  27. {"D", Direction.Down},
  28. };
  29.  
  30. public class Point
  31. {
  32. public int X { get; set; }
  33. public int Y { get; set; }
  34. public Point(int x, int y)
  35. {
  36. X = x;
  37. Y = y;
  38. }
  39. public override string ToString()
  40. {
  41. return $"X = {X} Y = {Y}";
  42. }
  43. }
  44. public class Nut
  45. {
  46. public Point Point { get; set; }
  47. public Point NextPoint { get; }
  48. public Direction Direction { get; set; }
  49. public bool Enabled { get; set; } = true;
  50. public Nut(IReadOnlyList<string> array) : this(int.Parse(array[0]), int.Parse(array[1]), dir[array[2]])
  51. {
  52.  
  53. }
  54. public Nut(int x, int y, Direction dir)
  55. {
  56. Point = new Point(x, y);
  57. NextPoint = new Point(x, y);
  58. Direction = dir;
  59. Refresh();
  60. }
  61. public void Refresh()
  62. {
  63. switch(Direction)
  64. {
  65. case Direction.Left: NextPoint.X = Point.X - 1; break;
  66. case Direction.Right: NextPoint.X = Point.X + 1; break;
  67. case Direction.Up: NextPoint.Y = Point.Y + 1; break;
  68. case Direction.Down: NextPoint.Y = Point.Y - 1; break;
  69. }
  70. }
  71.  
  72. public override bool Equals(object obj)
  73. {
  74. Nut nut = (Nut) obj;
  75. return NextPoint.X == nut.NextPoint.X && NextPoint.Y == nut.NextPoint.Y;
  76. }
  77.  
  78. public override string ToString()
  79. {
  80. return $"X = {Point.X} Y = {Point.Y}";
  81. }
  82. }
  83.  
  84. public static class Report
  85. {
  86. public static int Time { get; set; }
  87. public static int LastTime { get; set; }
  88. }
  89.  
  90.  
  91. static List<Nut> Nuts;
  92. static Dictionary<string, List<Nut>> Collisions = new Dictionary<string, List<Nut>>();
  93. static Point MaxPoint { get; set; }
  94. static void Main(string[] args)
  95. {
  96. int K = int.Parse(Reader.ReadLine());
  97. Nuts = new List<Nut>();
  98. int MaxX = 0, MaxY = 0;
  99. while (!Reader.EndOfStream)
  100. {
  101. Nuts.Add(new Nut(Reader.ReadLine().Split()));
  102. if (MaxX < Nuts.Last().Point.X) MaxX = Nuts.Last().Point.X;
  103. if (MaxY < Nuts.Last().Point.Y) MaxY = Nuts.Last().Point.Y;
  104. }
  105. MaxPoint = new Point(MaxX, MaxY);
  106. while(Nuts.Count > 1)
  107. {
  108. foreach (Nut nut in Nuts) Move(nut);
  109. ChangeDirection();
  110. Clear();
  111. }
  112. string line = $"{Report.Time:0.0}".Replace(',', '.');
  113. Writer.Write(line);
  114. Writer.Flush();
  115. }
  116.  
  117. static void Move(Nut nut)
  118. {
  119. if (!nut.Enabled) return;
  120. switch (nut.Direction)
  121. {
  122. case Direction.Left: nut.Point.X--; break;
  123. case Direction.Right: nut.Point.X++; break;
  124. case Direction.Up: nut.Point.Y++; break;
  125. case Direction.Down: nut.Point.Y--; break;
  126. }
  127. if (!Collisions.ContainsKey(nut.ToString()))
  128. Collisions[nut.ToString()] = new List<Nut> {nut};
  129. else
  130. Collisions[nut.ToString()].Add(nut);
  131. nut.Refresh();
  132. }
  133.  
  134. static void ChangeDirection()
  135. {
  136. Report.LastTime++;
  137. bool addTime = false;
  138. Direction direct;
  139. foreach (List<Nut> list in Collisions.Values)
  140. {
  141. switch (list.Count)
  142. {
  143. case 1:
  144. if (list[0].NextPoint.X > MaxPoint.X || list[0].NextPoint.Y > MaxPoint.Y || list[0].NextPoint.X < 0 || list[0].NextPoint.Y < 0)
  145. list[0].Enabled = false;
  146. break;
  147. case 2:
  148. direct = list[0].Direction;
  149. list[0].Direction = list[1].Direction;
  150. list[1].Direction = direct;
  151. addTime = true;
  152. break;
  153. case 3:
  154. Nut item;
  155. if (list[0].NextPoint.X == list[1].NextPoint.X || list[0].NextPoint.Y == list[1].NextPoint.Y)
  156. {
  157. item = list[2];
  158. direct = list[0].Direction;
  159. list[0].Direction = list[1].Direction;
  160. list[1].Direction = direct;
  161. }
  162. else if (list[0].NextPoint.X == list[2].NextPoint.X || list[0].NextPoint.Y == list[2].NextPoint.Y)
  163. {
  164. item = list[1];
  165. direct = list[0].Direction;
  166. list[0].Direction = list[2].Direction;
  167. list[2].Direction = direct;
  168. }
  169. else
  170. {
  171. item = list[0];
  172. direct = list[1].Direction;
  173. list[1].Direction = list[2].Direction;
  174. list[2].Direction = direct;
  175. }
  176. Move(item);
  177. addTime = true;
  178. break;
  179. case 4:
  180. if (list[0].NextPoint.X == list[1].NextPoint.X || list[0].NextPoint.Y == list[1].NextPoint.Y)
  181. {
  182. direct = list[0].Direction;
  183. list[0].Direction = list[1].Direction;
  184. list[1].Direction = direct;
  185. }
  186. else if (list[0].NextPoint.X == list[2].NextPoint.X || list[0].NextPoint.Y == list[2].NextPoint.Y)
  187. {
  188. direct = list[0].Direction;
  189. list[0].Direction = list[2].Direction;
  190. list[2].Direction = direct;
  191. }
  192. else if (list[0].NextPoint.X == list[3].NextPoint.X || list[0].NextPoint.Y == list[3].NextPoint.Y)
  193. {
  194. direct = list[0].Direction;
  195. list[0].Direction = list[3].Direction;
  196. list[3].Direction = direct;
  197. }
  198.  
  199. if (list[1].NextPoint.X == list[2].NextPoint.X || list[1].NextPoint.Y == list[2].NextPoint.Y)
  200. {
  201. direct = list[1].Direction;
  202. list[1].Direction = list[2].Direction;
  203. list[2].Direction = direct;
  204. }
  205. else if (list[1].NextPoint.X == list[3].NextPoint.X || list[1].NextPoint.Y == list[3].NextPoint.Y)
  206. {
  207. direct = list[1].Direction;
  208. list[1].Direction = list[3].Direction;
  209. list[3].Direction = direct;
  210. }
  211.  
  212. if (list[2].NextPoint.X == list[3].NextPoint.X || list[2].NextPoint.Y == list[3].NextPoint.Y)
  213. {
  214. direct = list[2].Direction;
  215. list[2].Direction = list[3].Direction;
  216. list[3].Direction = direct;
  217. }
  218. addTime = true;
  219. break;
  220. }
  221.  
  222. }
  223. if (addTime)
  224. Report.Time = Report.LastTime;
  225. }
  226. static void Clear()
  227. {
  228. for(int i = 0; i < Nuts.Count; i++)
  229. if (!Nuts[i].Enabled)
  230. Nuts.RemoveAt(i--);
  231. Collisions.Clear();
  232. }
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement