Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Program
- {
- static StreamReader Reader = new StreamReader(@"stdin");
- static StreamWriter Writer = new StreamWriter(@"stdout");
- public enum Direction
- {
- Left,
- Right,
- Up,
- Down
- }
- static Dictionary<string, Direction> dir = new Dictionary<string, Direction>()
- {
- {"L", Direction.Left},
- {"R", Direction.Right},
- {"U", Direction.Up},
- {"D", Direction.Down},
- };
- public class Point
- {
- public int X { get; set; }
- public int Y { get; set; }
- public Point(int x, int y)
- {
- X = x;
- Y = y;
- }
- public override string ToString()
- {
- return $"X = {X} Y = {Y}";
- }
- }
- public class Nut
- {
- public Point Point { get; set; }
- public Point NextPoint { get; }
- public Direction Direction { get; set; }
- public bool Enabled { get; set; } = true;
- public Nut(IReadOnlyList<string> array) : this(int.Parse(array[0]), int.Parse(array[1]), dir[array[2]])
- {
- }
- public Nut(int x, int y, Direction dir)
- {
- Point = new Point(x, y);
- NextPoint = new Point(x, y);
- Direction = dir;
- Refresh();
- }
- public void Refresh()
- {
- switch(Direction)
- {
- case Direction.Left: NextPoint.X = Point.X - 1; break;
- case Direction.Right: NextPoint.X = Point.X + 1; break;
- case Direction.Up: NextPoint.Y = Point.Y + 1; break;
- case Direction.Down: NextPoint.Y = Point.Y - 1; break;
- }
- }
- public override bool Equals(object obj)
- {
- Nut nut = (Nut) obj;
- return NextPoint.X == nut.NextPoint.X && NextPoint.Y == nut.NextPoint.Y;
- }
- public override string ToString()
- {
- return $"X = {Point.X} Y = {Point.Y}";
- }
- }
- public static class Report
- {
- public static int Time { get; set; }
- public static int LastTime { get; set; }
- }
- static List<Nut> Nuts;
- static Dictionary<string, List<Nut>> Collisions = new Dictionary<string, List<Nut>>();
- static Point MaxPoint { get; set; }
- static void Main(string[] args)
- {
- int K = int.Parse(Reader.ReadLine());
- Nuts = new List<Nut>();
- int MaxX = 0, MaxY = 0;
- while (!Reader.EndOfStream)
- {
- Nuts.Add(new Nut(Reader.ReadLine().Split()));
- if (MaxX < Nuts.Last().Point.X) MaxX = Nuts.Last().Point.X;
- if (MaxY < Nuts.Last().Point.Y) MaxY = Nuts.Last().Point.Y;
- }
- MaxPoint = new Point(MaxX, MaxY);
- while(Nuts.Count > 1)
- {
- foreach (Nut nut in Nuts) Move(nut);
- ChangeDirection();
- Clear();
- }
- string line = $"{Report.Time:0.0}".Replace(',', '.');
- Writer.Write(line);
- Writer.Flush();
- }
- static void Move(Nut nut)
- {
- if (!nut.Enabled) return;
- switch (nut.Direction)
- {
- case Direction.Left: nut.Point.X--; break;
- case Direction.Right: nut.Point.X++; break;
- case Direction.Up: nut.Point.Y++; break;
- case Direction.Down: nut.Point.Y--; break;
- }
- if (!Collisions.ContainsKey(nut.ToString()))
- Collisions[nut.ToString()] = new List<Nut> {nut};
- else
- Collisions[nut.ToString()].Add(nut);
- nut.Refresh();
- }
- static void ChangeDirection()
- {
- Report.LastTime++;
- bool addTime = false;
- Direction direct;
- foreach (List<Nut> list in Collisions.Values)
- {
- switch (list.Count)
- {
- case 1:
- if (list[0].NextPoint.X > MaxPoint.X || list[0].NextPoint.Y > MaxPoint.Y || list[0].NextPoint.X < 0 || list[0].NextPoint.Y < 0)
- list[0].Enabled = false;
- break;
- case 2:
- direct = list[0].Direction;
- list[0].Direction = list[1].Direction;
- list[1].Direction = direct;
- addTime = true;
- break;
- case 3:
- Nut item;
- if (list[0].NextPoint.X == list[1].NextPoint.X || list[0].NextPoint.Y == list[1].NextPoint.Y)
- {
- item = list[2];
- direct = list[0].Direction;
- list[0].Direction = list[1].Direction;
- list[1].Direction = direct;
- }
- else if (list[0].NextPoint.X == list[2].NextPoint.X || list[0].NextPoint.Y == list[2].NextPoint.Y)
- {
- item = list[1];
- direct = list[0].Direction;
- list[0].Direction = list[2].Direction;
- list[2].Direction = direct;
- }
- else
- {
- item = list[0];
- direct = list[1].Direction;
- list[1].Direction = list[2].Direction;
- list[2].Direction = direct;
- }
- Move(item);
- addTime = true;
- break;
- case 4:
- if (list[0].NextPoint.X == list[1].NextPoint.X || list[0].NextPoint.Y == list[1].NextPoint.Y)
- {
- direct = list[0].Direction;
- list[0].Direction = list[1].Direction;
- list[1].Direction = direct;
- }
- else if (list[0].NextPoint.X == list[2].NextPoint.X || list[0].NextPoint.Y == list[2].NextPoint.Y)
- {
- direct = list[0].Direction;
- list[0].Direction = list[2].Direction;
- list[2].Direction = direct;
- }
- else if (list[0].NextPoint.X == list[3].NextPoint.X || list[0].NextPoint.Y == list[3].NextPoint.Y)
- {
- direct = list[0].Direction;
- list[0].Direction = list[3].Direction;
- list[3].Direction = direct;
- }
- if (list[1].NextPoint.X == list[2].NextPoint.X || list[1].NextPoint.Y == list[2].NextPoint.Y)
- {
- direct = list[1].Direction;
- list[1].Direction = list[2].Direction;
- list[2].Direction = direct;
- }
- else if (list[1].NextPoint.X == list[3].NextPoint.X || list[1].NextPoint.Y == list[3].NextPoint.Y)
- {
- direct = list[1].Direction;
- list[1].Direction = list[3].Direction;
- list[3].Direction = direct;
- }
- if (list[2].NextPoint.X == list[3].NextPoint.X || list[2].NextPoint.Y == list[3].NextPoint.Y)
- {
- direct = list[2].Direction;
- list[2].Direction = list[3].Direction;
- list[3].Direction = direct;
- }
- addTime = true;
- break;
- }
- }
- if (addTime)
- Report.Time = Report.LastTime;
- }
- static void Clear()
- {
- for(int i = 0; i < Nuts.Count; i++)
- if (!Nuts[i].Enabled)
- Nuts.RemoveAt(i--);
- Collisions.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement