Ejemplo n.º 1
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var dijkstra  = new DijkstraPathFinder();
            var paths     = dijkstra.GetPathsByDijkstra(state, state.Position, state.Chests);
            var stack     = new Stack <PathWithCost>(paths.Reverse());
            var firstNode = new Node(stack.Peek().Path, stack.Peek().Cost);
            var energy    = state.Energy;

            while (stack.Count != 0)
            {
                var currentPath = stack.Pop();
                if (currentPath.Cost > energy)
                {
                    continue;
                }
                var startNode = new Node(currentPath.Path, currentPath.Cost);
                foreach (var path in dijkstra.GetPathsByDijkstra(state, currentPath.End, state.Chests.Where(x => !x.Equals(currentPath.End))))
                {
                    if (startNode.Value + path.Cost < energy)
                    {
                        energy -= startNode.Value + path.Cost;
                        stack.Push(path);
                    }
                }
            }


            return(null);
        }
Ejemplo n.º 2
0
        private static void CreatePath(State state, Point start, List <Point> notVisitedTargets,
                                       PathWithCost resultPath, PathWithTarget bestPath)
        {
            CorrectBestPath(bestPath, resultPath, notVisitedTargets);

            var pathFinder = new DijkstraPathFinder();
            var paths      = pathFinder.GetPathsByDijkstra(state, start, notVisitedTargets);

            foreach (var path in paths)
            {
                var cost = resultPath.Cost + path.Cost;
                if (cost <= state.Energy)
                {
                    notVisitedTargets.Remove(path.End);

                    var way = resultPath.Path.ToList();
                    way.AddRange(path.Path.Skip(1));
                    CreatePath(state, path.End, notVisitedTargets,
                               new PathWithCost(cost, way.ToArray()), bestPath);

                    if (bestPath.NumberOfTargets == 0)
                    {
                        return;
                    }

                    notVisitedTargets.Add(path.End);
                }
            }
        }
Ejemplo n.º 3
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var result   = new List <Point>();
            var position = state.Position;
            var chests   = new List <Point>(state.Chests);
            var goal     = state.Goal;

            while (goal-- != 0)
            {
                var dijkstraPathFinder = new DijkstraPathFinder();
                var test = dijkstraPathFinder.GetPathsByDijkstra(state, position, chests);
                if (!test.Any())
                {
                    return(new List <Point>());
                }
                var path = test.First();
                position = path.Path.Last();
                path.Path.RemoveAt(0);
                if (path.Cost > state.Energy)
                {
                    return(new List <Point>());
                }
                result.AddRange(path.Path);
                chests.Remove(position);
            }
            return(result);
        }
Ejemplo n.º 4
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            if (state.Chests.Count < state.Goal)
            {
                return(new List <Point>());
            }
            var path   = new List <Point>();
            var start  = state.Position;
            var chests = new HashSet <Point>(state.Chests);
            var finder = new DijkstraPathFinder();

            for (var i = 0; i < state.Goal; i++)
            {
                var shortestPath = finder.GetPathsByDijkstra(state, start, chests).FirstOrDefault();
                if (shortestPath == null)
                {
                    return(new List <Point>());
                }
                if (state.Energy < shortestPath.Cost)
                {
                    return(new List <Point>());
                }
                path.AddRange(shortestPath.Path.Skip(1));
                chests.Remove(shortestPath.End);
                start = shortestPath.End;
            }

            return(path);
        }
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var result = new List <Point>();

            if (state.Goal > state.Chests.Count)
            {
                return(result);
            }
            var dijkstraPath = new DijkstraPathFinder();
            var energy       = 0;
            var target       = new HashSet <Point>(state.Chests);
            var position     = state.Position;

            for (var i = 0; i < state.Goal; i++)
            {
                var pathWithCost = dijkstraPath.GetPathsByDijkstra(state, position, target).FirstOrDefault();
                energy += pathWithCost.Cost;
                if (state.Energy < pathWithCost.Cost || pathWithCost == default(PathWithCost))
                {
                    break;
                }
                var path = pathWithCost.Path;
                position = path.Last();
                target.Remove(position);
                result.AddRange(path);
            }

            return(result);
        }
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var currentEnergy = 0;
            var currentGoal   = 0;
            var chests        = state.Chests.ToHashSet();
            var pathFinder    = new DijkstraPathFinder();
            var path          = new List <Point>();
            var start         = state.Position;

            while (currentEnergy < state.Energy && currentGoal < state.Goal)
            {
                var pathsWithCosts = pathFinder.GetPathsByDijkstra(state, start, chests);

                var shortestWay = pathsWithCosts.FirstOrDefault();
                if (shortestWay == null)
                {
                    return(new List <Point>());
                }

                currentEnergy += shortestWay.Cost;
                currentGoal++;
                path.AddRange(shortestWay.Path.Skip(1));
                start = shortestWay.End;
                chests.Remove(start);
            }

            if (currentGoal < state.Goal || currentEnergy > state.Energy)
            {
                return(new List <Point>());
            }

            return(path);
        }
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var chests     = state.Chests.ToHashSet();
            var goal       = 0;
            var myEnergy   = 0;
            var pathFinder = new DijkstraPathFinder();
            var path       = new List <Point>();
            var start      = state.Position;

            while (myEnergy < state.Energy && goal < state.Goal)
            {
                var roadsByDijkstra = pathFinder.GetPathsByDijkstra(state, start, chests);

                var shortestWay = roadsByDijkstra.FirstOrDefault();
                if (shortestWay == null)
                {
                    return(new List <Point>());
                }

                myEnergy += shortestWay.Cost;
                goal++;
                path.AddRange(shortestWay.Path.Skip(1));
                start = shortestWay.End;
                chests.Remove(start);
            }

            return(goal < state.Goal || myEnergy > state.Energy ? new List <Point>() : path);
        }
Ejemplo n.º 8
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var notVisitedChests = state.Chests.ToList();
            var start            = state.Position;
            var energy           = state.Energy;
            var resultPath       = new List <Point>();
            var pathFinder       = new DijkstraPathFinder();

            while (state.Chests.Count - notVisitedChests.Count != state.Goal)
            {
                var path         = pathFinder.GetPathsByDijkstra(state, start, notVisitedChests);
                var shortestPath = path.FirstOrDefault();
                if (shortestPath == null)
                {
                    return(new List <Point>());
                }

                energy -= shortestPath.Cost;
                if (energy < 0)
                {
                    return(new List <Point>());
                }

                start = shortestPath.End;
                resultPath.AddRange(shortestPath.Path.Skip(1));
                notVisitedChests.Remove(shortestPath.End);
            }
            return(resultPath);
        }
Ejemplo n.º 9
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            var finalFindedPath = new List <Point>();
            var startPoint      = state.Position;
            var currentEnegry   = state.InitialEnergy;
            var targets         = state.Chests.ToHashSet();

            if (targets.Count() < state.Goal)
            {
                return(new List <Point>());
            }
            if (targets.Contains(startPoint))
            {
                state.Scores++;
                targets.Remove(startPoint);
            }
            while (state.Scores < state.Goal)
            {
                var finderPathClass   = new DijkstraPathFinder();
                var allPathsToTargets = finderPathClass.GetPathsByDijkstra(state, startPoint, targets);
                if (allPathsToTargets.FirstOrDefault() == null)
                {
                    return(new List <Point>());
                }
                var easyPathWithCost = allPathsToTargets.First();
                var energyToEasyPath = easyPathWithCost.Cost;
                var easyPath         = easyPathWithCost.Path.Skip(1);
                if (currentEnegry < energyToEasyPath)
                {
                    return(new List <Point>());
                }
                foreach (var point in easyPath)
                {
                    finalFindedPath.Add(point);
                }
                var lastPoint = easyPath.Last();
                startPoint     = lastPoint;
                currentEnegry -= energyToEasyPath;
                targets.Remove(lastPoint);
                state.Scores++;
            }
            return(finalFindedPath);
        }
Ejemplo n.º 10
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            if (state.Goal == 0)
            {
                return(new List <Point>());
            }

            HashSet <Point>    chests = new HashSet <Point>(state.Chests);
            DijkstraPathFinder finder = new DijkstraPathFinder();
            List <Point>       result = new List <Point>();

            var currentCost = 0;
            var position    = state.Position;

            for (int i = 0; i < state.Goal; i++)
            {
                if (!chests.Any())
                {
                    return(new List <Point>());
                }
                PathWithCost pathToNewChest = finder.GetPathsByDijkstra(state, position, chests).FirstOrDefault();
                if (pathToNewChest == null)
                {
                    return(new List <Point>());
                }
                position     = pathToNewChest.End;
                currentCost += pathToNewChest.Cost;
                if (currentCost > state.Energy)
                {
                    return(new List <Point>());
                }
                chests.Remove(pathToNewChest.End);

                for (int j = 1; j < pathToNewChest.Path.Count; j++)
                {
                    result.Add(pathToNewChest.Path[j]);
                }
            }

            return(result);
        }
Ejemplo n.º 11
0
        public List <Point> FindPathToCompleteGoal(State state)
        {
            if (state.Goal > state.Chests.Count)
            {
                return(new List <Point>());
            }

            var dijkstra          = new DijkstraPathFinder();
            var notVisitedChests  = new HashSet <Point>(state.Chests);
            var visitedChestCount = 0;
            var currentEnergy     = 0;
            var currentCell       = state.Position;
            var pathToGoal        = new List <Point>();

            while (true)
            {
                var path = dijkstra.GetPathsByDijkstra(state, currentCell, notVisitedChests).FirstOrDefault();
                if (path == null)
                {
                    return(new List <Point>());
                }

                notVisitedChests.Remove(path.End);
                ++visitedChestCount;
                currentEnergy += path.Cost;
                currentCell    = path.End;
                pathToGoal.AddRange(path.Path.Skip(1));

                if (visitedChestCount == state.Goal && currentEnergy <= state.Energy)
                {
                    break;
                }
                if (currentEnergy >= state.Energy)
                {
                    return(new List <Point>());
                }
            }

            return(pathToGoal);
        }