コード例 #1
0
        public IEnumerable <RobotAction> GetNextActions(Robot robot)
        {
            // find the closest unwrapped cells
            var finder = new SimpleTargetSelector(Problem.Map);

            var targets = finder.GetPotentialTargets(robot.Position, 1);

            // sort them by moves
            var bestScore = int.MaxValue;
            Queue <RobotAction> bestRoute = null;

            if (targets.Count == 0)
            {
                return new[] { RobotAction.Done }
            }
            ;

            foreach (var p in targets)
            {
                var route = AStarPathFinder.GetRouteTo(robot.Position, p, Problem.Map);

                if (route.Count <= bestScore)
                {
                    bestScore    = route.Count;
                    bestRoute    = route;
                    robot.Target = p;
                }
            }

            if (robot.PriorTarget != null && robot.Target != robot.PriorTarget)
            {
                var priorRoute = AStarPathFinder.GetRouteTo(robot.Position, robot.PriorTarget.Value, Problem.Map);
                if (priorRoute.Count != 0)
                {
                    if (priorRoute.Count <= bestScore)
                    {
                        return(priorRoute);
                    }
                }
            }

            robot.PriorTarget = robot.Target;
            return(bestRoute);
        }
    }
コード例 #2
0
        public IEnumerable <RobotAction> GetNextActions(Robot robot)
        {
            robot.Targets = null;
            robot.Target  = null;

            // gimme all the empty cells within x moves
            var empties = DijkstraPathfinder.FindUnwrappedCellsWithin(robot.Position, Problem.Map, 10, false);

            if (empties.Count == 0)
            {
                return(NextController.GetNextActions(robot));
            }

            var islands    = new Dictionary <Point, HashSet <Point> >();
            var notIslands = new HashSet <Point>();

            foreach (var t in empties)
            {
                // point is not in an island cluster
                if (notIslands.Contains(t))
                {
                    continue;
                }

                // point is already in an island
                bool found = false;
                foreach (var island in islands.Values)
                {
                    if (island.Contains(t))
                    {
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }

                var np = DijkstraPathfinder.FindUnwrappedCellsWithin(t, Problem.Map, 100, true);
                if (np.Count > 50)
                {
                    np.ToList().ForEach(x => notIslands.Add(x)); // not an island, mark 'em
                }
                else
                {
                    // found a new island
                    islands.Add(t, new HashSet <Point>(np));
                }
            }

            // no islands, do something else
            if (islands.Count == 0)
            {
                return(NextController.GetNextActions(robot));
            }

            // paint islands
            robot.Targets = new HashSet <Point>(islands.Values.SelectMany(x => x));

            // find the closest point in the smallest island
            var closestScore = int.MaxValue;
            IEnumerable <RobotAction> closestRoute = null;

            foreach (var target in islands.OrderBy(x => x.Value.Count).First().Value)
            {
                var result = AStarPathFinder.GetRouteTo(robot.Position, target, Problem.Map, closestScore);

                if (result != null && result.Count < closestScore)
                {
                    closestScore = result.Count;
                    closestRoute = result;
                    robot.Target = target;
                }
            }

            // go to there
            return(closestRoute.Take(1));
        }