Ejemplo n.º 1
0
        public Solver(string inputFile)
        {
            Map = new Map();

            var input = File.OpenText(inputFile);

            var size = input.ReadLine().Split(' ').Select(int.Parse).ToArray();

            Map.Width = size[0];
            Map.Height = size[1];

            var infos = input.ReadLine().Split(' ').Select(int.Parse).ToArray();

            Map.TargetCount = infos[0];
            Map.DroneCount = infos[1];
            Map.MaxMove = infos[2];
            Map.TurnCount = infos[3];

            var start = input.ReadLine().Split(' ').Select(int.Parse).ToArray();

            Map.StartPosition = new Point(start[1], start[0]);

            Map.Targets = Enumerable.Range(0, Map.TargetCount)
                .Select(i => input.ReadLine().Split(' ').Select(int.Parse).ToArray())
                .Select(i => new Point(i[1], i[0]))
                .ToList();

            Map.Drones = Enumerable.Range(0, Map.DroneCount)
                .Select(d => new Drone
                {
                    Index = d,
                    Position = Map.StartPosition,
                })
                .ToList();
        }
Ejemplo n.º 2
0
        public static IEnumerable<Moves> GetMovesFromPath(Map map, IEnumerable<Point> shortestPath)
        {
            var previous = shortestPath.First();

            foreach (var node in shortestPath.Skip(1))
            {
                if (node.X < previous.X || (node.X == map.Width - 1 && previous.X == 0))
                    yield return Moves.Left;
                else if (node.X > previous.X || (node.X == 0 && previous.X == map.Width - 1))
                    yield return Moves.Right;
                else if (node.Y > previous.Y)
                    yield return Moves.Down;
                else if (node.Y < previous.Y)
                    yield return Moves.Up;
                else
                    Debugger.Break();

                previous = node;
            }
        }
Ejemplo n.º 3
0
        public static Target GetDistance(Map map, Point source, Point target)
        {
            var distanceWithOverflow = map.Width
                - Math.Max(source.X, target.X)
                + Math.Min(source.X, target.X);

            var distanceWithoutOverflow = Math.Abs(source.X - target.X);

            var useOverflow = distanceWithOverflow < distanceWithoutOverflow;

            var distanceVertical = Math.Abs(source.Y - target.Y);

            var distance = Math.Min(distanceWithOverflow, distanceWithoutOverflow) + distanceVertical;

            return new Target()
            {
                Position = target,
                Distance = distance,
                OverflowPath = useOverflow,
            };
        }
Ejemplo n.º 4
0
        public static IEnumerable<Point> GetNeightboors(Map map, Point point, IEnumerable<Point> avoid)
        {
            var left = new Point(point.X - 1, point.Y);
            var right = new Point(point.X + 1, point.Y);
            var top = new Point(point.X, point.Y - 1);
            var bot = new Point(point.X, point.Y + 1);

            if (left.X == -1)
                left = new Point(map.Width - 1, point.Y);
            else if (right.X == map.Width)
                right = new Point(0, point.Y);

            if (!avoid.Contains(left))
                yield return left;

            if (!avoid.Contains(right))
                yield return right;

            if (!avoid.Contains(top))
                yield return top;

            if (!avoid.Contains(bot))
                yield return bot;
        }