Beispiel #1
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var straightWay        = GetStraightWay(map).ToList();
            var straightDirections = ConvertPointsToDirections(straightWay).ToArray();

            if (straightWay.Any(point => map.Chests.Contains(point)))
            {
                return(straightDirections);
            }

            var pathsToPlayer = BfsTask.FindPaths(map, map.InitialPosition, map.Chests).ToList();

            if (!pathsToPlayer.Any())
            {
                return(straightDirections);
            }
            var pathsToExit = BfsTask.FindPaths(map, map.Exit, map.Chests);
            var fullPaths   = pathsToPlayer.Join(pathsToExit, list => list.Value, list => list.Value,
                                                 (l1, l2) => l1
                                                 .Reverse()
                                                 .Concat(l2.Skip(1))
                                                 .ToList()).ToList();

            if (!fullPaths.Any())
            {
                return(new MoveDirection[0]);
            }

            var shortestPath = fullPaths.Aggregate((min, x) => x.Count < min.Count ? x : min);

            return(ConvertPointsToDirections(shortestPath).ToArray());
        }
Beispiel #2
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var wayFromStartToExit = BfsTask.FindPaths(map, map.InitialPosition, new Point[] { map.Exit }).FirstOrDefault();

            if (wayFromStartToExit != null)
            {
                if (map.Chests.Any(pointChest => wayFromStartToExit.Contains(pointChest)))
                {
                    return(GetMoveDirectionArray(wayFromStartToExit.Reverse().ToList()));
                }
                else
                {
                    var wayFromStartToChest = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
                    var mergeWay            = wayFromStartToChest
                                              .Select(single => Tuple.Create(single, BfsTask.FindPaths(map, single.Value, new[] { map.Exit }).FirstOrDefault()));
                    var result = GetMergeMinList(mergeWay);

                    if (result == null)
                    {
                        return(GetMoveDirectionArray(wayFromStartToExit.Reverse().ToList()));
                    }

                    return(GetMoveDirectionArray(result));
                }
            }
            else
            {
                return(new MoveDirection[0]);
            }
        }
Beispiel #3
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var exitWay = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit }).FirstOrDefault();

            if (exitWay == null)
            {
                return(new MoveDirection[0]);
            }

            if (map.Chests.Any(box => exitWay.ToList().Contains(box)))
            {
                return(exitWay.ToList().SortingOut());
            }

            var boxes   = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
            var outcome = boxes.Select(box => Tuple.Create(
                                           box, BfsTask.FindPaths(map, box.Value, new[] { map.Exit }).FirstOrDefault()))
                          .MinElement();

            if (outcome == null)
            {
                return(exitWay.ToList().SortingOut());
            }

            return(outcome.Item1.ToList().SortingOut().Concat(
                       outcome.Item2.ToList().SortingOut())
                   .ToArray());
        }
Beispiel #4
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            HashSet <Point> chests     = new HashSet <Point>(map.Chests);
            var             pathToExit = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit }).FirstOrDefault();

            if (pathToExit != null)
            {
                foreach (var point in pathToExit)
                {
                    if (chests.Contains(point))
                    {
                        return(MakeDirectionFromPath(pathToExit));
                    }
                }
            }
            var pathsToChests = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
            SinglyLinkedList <Point> shortestPath = null;

            foreach (var pathsToChest in pathsToChests)
            {
                var pathFromChestToExit = FindPath(map, pathsToChest, map.Exit);
                if (shortestPath == null || (pathFromChestToExit != null && pathFromChestToExit.Length < shortestPath.Length))
                {
                    shortestPath = pathFromChestToExit;
                }
            }
            if (shortestPath == null)
            {
                shortestPath = pathToExit;
            }
            return(MakeDirectionFromPath(shortestPath));
        }
Beispiel #5
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var shortestPaths = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit });

            if (shortestPaths.Any())
            {
                var shortestPath = shortestPaths.First();
                if (shortestPath.Any(point => map.Chests.Contains(point)))
                {
                    return(GetMoveDirection(shortestPath.Reverse().ToArray()));
                }
                var pathsFromInitial  = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
                var pathsFromExit     = BfsTask.FindPaths(map, map.Exit, map.Chests);
                var pathsThroughChest = pathsFromInitial.Join(pathsFromExit,
                                                              path1 => path1.Value,
                                                              path2 => path2.Value,
                                                              (path1, path2) => path1.Reverse().Concat(path2.Skip(1)))
                                        .OrderBy(p => p.Count());
                if (pathsThroughChest.Any())
                {
                    return(GetMoveDirection(pathsThroughChest.First().ToArray()));
                }
                return(GetMoveDirection(shortestPath.Reverse().ToArray()));
            }
            return(new MoveDirection[0]);
        }
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var roadsFromStart = BfsTask
                                 .FindPaths(map, map.InitialPosition, map.Chests);
            var roadsFromExit = BfsTask
                                .FindPaths(map, map.Exit, map.Chests);
            var lengths   = new List <int>();
            var fullpaths = roadsFromExit
                            .Join(roadsFromStart,
                                  x => x.Value,
                                  y => y.Value,
                                  (x, y) =>
            {
                lengths.Add(x.Length + y.Length - 1);
                return(y.Reverse().Concat(x.Skip(1)));
            });

            fullpaths.All(x => true);

            if (fullpaths.Any())
            {
                return(TransformToDirections(fullpaths.Skip(lengths.IndexOf(lengths.Min())).First()));
            }

            fullpaths = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit });
            return(fullpaths.Any() ? TransformToDirections(fullpaths.First().Reverse()) : new MoveDirection[0]);
        }
Beispiel #7
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var shortetsPath = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit });

            if (shortetsPath.Any())
            {
                var firstShortestPath = shortetsPath.First();
                if (firstShortestPath.Any(x => map.Chests.Contains(x)))
                {
                    return(PointsToMoveDirection(firstShortestPath.Reverse().ToArray()));
                }

                var shortestPathFromInitial = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);

                var shortestPathFromCastle = BfsTask.FindPaths(map, map.Exit, map.Chests);

                var shortestPathThroughtChest = shortestPathFromInitial.Join(shortestPathFromCastle,
                                                                             x => x.Value,
                                                                             y => y.Value,
                                                                             (x, y) => x.Reverse().Concat(y.Skip(1))).OrderBy(x => x.Count());

                if (!shortestPathThroughtChest.Any())
                {
                    return(PointsToMoveDirection(shortetsPath.First().Reverse().ToArray()));
                }
                return(PointsToMoveDirection(shortestPathThroughtChest.First().ToArray()));
            }
            return(new MoveDirection[0]);
        }
Beispiel #8
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            //Путь до выхода
            var pathToExit = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit }).FirstOrDefault();

            //Если нет пути до выхода
            if (pathToExit == null)
            {
                return(new MoveDirection[0]);
            }

            //Если найденый путь до выхода содержит хоть один сундук
            if (map.Chests.Any(chest => pathToExit.ToList().Contains(chest)))
            {
                return(pathToExit.ToList().ParseToDirections());
            }

            //Находим кратчайший путь
            var chests = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
            var result = chests.Select(chest => Tuple.Create(
                                           chest, BfsTask.FindPaths(map, chest.Value, new[] { map.Exit }).FirstOrDefault()))
                         .MinElement();

            //Если кратчайший путь не проходит ни через один сундук
            if (result == null)
            {
                return(pathToExit.ToList().ParseToDirections());
            }

            //Парсим каждую часть пути (до сундука и от него) в путь MoveDirection и соединяем
            return(result.Item1.ToList().ParseToDirections().Concat(
                       result.Item2.ToList().ParseToDirections())
                   .ToArray());
        }
Beispiel #9
0
        private static List <Point>[] GetPaths(Map map)
        {
            var paths = BfsTask.FindPaths(map, map.InitialPosition, map.Chests)
                        .Select(x => x.ToList())
                        .ToArray();

            return(paths);
        }
Beispiel #10
0
        private static IEnumerable <Point> GetStraightWay(Map map)
        {
            var path =
                BfsTask.FindPaths(map, map.InitialPosition, new[] { new Point(map.Exit.X, map.Exit.Y) }).ToList();

            return(path.Any()
                ? path[0].Reverse()
                : new List <Point>());
        }
 public void OnMouseDown(Point location)
 {
     lastMouseClick = new Point(location.X, location.Y);
     pathsToChests  = null;
     if (currentMap.InBounds(location) && currentMap.Dungeon[lastMouseClick.Value.X, lastMouseClick.Value.Y] == MapCell.Empty)
     {
         pathsToChests = BfsTask.FindPaths(currentMap, lastMouseClick.Value, currentMap.Chests)
                         .Select(x => x.ToList()).ToList();
         foreach (var pathsToChest in pathsToChests)
         {
             pathsToChest.Reverse();
         }
     }
 }
        public static IEnumerable <Point> GetShortestPath(this IEnumerable <SinglyLinkedList <Point> > startToChests,
                                                          Map map)
        {
            var exitToChests = BfsTask.FindPaths(map, map.Exit, map.Chests);

            var startToChestToExit = startToChests
                                     .Join(exitToChests, sToCh => sToCh.Value, eToCh => eToCh.Value,
                                           (sToCh, eToCh) =>
            {
                var connect = new SinglyLinkedList <Point>(eToCh.Value, sToCh);
                return(eToCh.Skip(1).Aggregate(connect,
                                               (current, point) => new SinglyLinkedList <Point>(point, current)));
            })
                                     .Where(sTcTe => sTcTe != null);

            return(startToChestToExit.OrderBy(list => list.Length).First());
        }
Beispiel #13
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var path        = new MoveDirection[0];
            var pathToChest = BfsTask.FindPaths(map, map.InitialPosition, map.Chests).FirstOrDefault();
            var exit        = new Point[] { map.Exit };
            var pathToExit  = BfsTask.FindPaths(map, pathToChest.Value, exit).FirstOrDefault();

            if (pathToChest != null && pathToExit != null)
            {
                //path = (ExtractToPath(pathToChest).Concat(ExtractToPath(pathToExit))).ToArray();
                var path1 = ExtractToPath(pathToChest).ToArray();
                var path2 = ExtractToPath(pathToExit).ToArray();

                path = path1.Concat(path2).ToArray();
            }

            return(path);
        }
Beispiel #14
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var start  = map.InitialPosition;
            var exit   = map.Exit;
            var chests = map.Chests;
            var pathFromStartToChest          = BfsTask.FindPaths(map, start, chests);
            var pathFromExitToChest           = BfsTask.FindPaths(map, exit, chests);
            var pathFromSatrtToExitThrowChest = pathFromStartToChest.Join(pathFromExitToChest,
                                                                          path1 => path1.Value, path2 => path2.Value, (path1, path2) => Tuple.Create(path1, path2));

            if (!pathFromSatrtToExitThrowChest.Any())
            {
                var pathFromStartToExit = BfsTask.FindPaths(map, start, new[] { map.Exit }).FirstOrDefault();
                return((pathFromStartToExit == null) ? new MoveDirection[0] : ConvertPathToDirection(pathFromStartToExit.Reverse().ToList()));
            }
            var shortestPath     = pathFromSatrtToExitThrowChest.OrderBy(path => path.Item1.Count() + path.Item2.Count()).First();
            var shortestPathList = shortestPath.Item1.Reverse().Concat(shortestPath.Item2.Skip(1)).ToList();

            return(ConvertPathToDirection(shortestPathList));
        }
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var startToExit = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit }).FirstOrDefault();

            if (startToExit == null)
            {
                return(new MoveDirection[0]);
            }
            if (map.Chests.Any(chest => startToExit.Contains(chest)))
            {
                return(startToExit.PointsToMoveDirection().ToArray());
            }
            var startToChests = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);

            if (!startToChests.Any())
            {
                return(startToExit.PointsToMoveDirection().ToArray());
            }

            return(startToChests.GetShortestPath(map).PointsToMoveDirection().ToArray());
        }
Beispiel #16
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var pathStart = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
            var pathEnd   = BfsTask.FindPaths(map, map.Exit, map.Chests);
            var listStart = PathCollectionToListPath(pathStart, true);
            var listEnd   = PathCollectionToListPath(pathEnd, false);
            var result    = JoinStartPathAndEndPath(listStart, listEnd);

            if (result.Count() == 0)
            {
                var path = BfsTask.FindPaths(map, map.InitialPosition, new Point[] { map.Exit });
                result = PathCollectionToListPath(path, true).ToList();
            }
            if (result.Count() == 0)
            {
                return(new MoveDirection[0]);
            }
            List <Point> shortesPath       = SearchShortesPath(result.ToList());
            var          pathMoveDirection = PathConvertToMoveDirection(shortesPath);

            return(pathMoveDirection.ToArray());
        }
Beispiel #17
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var pathsToChest = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
            var dictChest    = pathsToChest.ToDictionary(way => way.Value, way => way.Count());

            var pathsToExit = BfsTask.FindPaths(map, map.Exit, pathsToChest.Count() > 0 ? dictChest.Keys.ToArray() : new[] { map.InitialPosition });

            if (pathsToExit.Count() == 0)
            {
                return(new MoveDirection[0]);
            }
            var dictExit = pathsToExit.ToDictionary(way => way.Value, way => way.Count());

            var bestWay = dictChest.OrderBy(w => w.Value + dictExit[w.Key]).FirstOrDefault().Key;

            var pathChest = pathsToChest.FirstOrDefault(p => p.Value == bestWay);
            var pathExit  = pathsToExit.FirstOrDefault(p => p.Value == bestWay);

            return(pathChest == null?
                   GetMovesByWay(pathExit).ToArray() :
                       GetMovesByWay(pathChest.Reverse()).Concat(GetMovesByWay(pathExit)).ToArray());
        }
Beispiel #18
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var pathsFromInitial = BfsTask.FindPaths(
                map, map.InitialPosition, map.Chests);
            var pathsFromExit = BfsTask.FindPaths(
                map, map.Exit, map.Chests);

            var pathsFromInitialToExit = pathsFromInitial.Join(
                pathsFromExit,
                x => x.Value,
                y => y.Value,
                (x, y) => new DungeonPath
            {
                List   = x.Reverse().Concat(y.Skip(1)),
                Length = x.Length + y.Length - 1,
            });

            var shortestPath = GetShortestPath(pathsFromInitialToExit);

            if (shortestPath.Any())
            {
                return(TransformToDirection(shortestPath));
            }

            pathsFromInitialToExit = BfsTask
                                     .FindPaths(map, map.InitialPosition, new[] { map.Exit })
                                     .Select(x => new DungeonPath
            {
                List   = x.Reverse(),
                Length = x.Length,
            });

            shortestPath = GetShortestPath(pathsFromInitialToExit);

            return(shortestPath.Any() ? TransformToDirection(shortestPath) : new MoveDirection[0]);
        }
Beispiel #19
0
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var pathFromStartToExit = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit })
                                      .FirstOrDefault();

            if (pathFromStartToExit == null)
            {
                return(new MoveDirection[0]);
            }

            if (map.Chests.Any(chest => pathFromStartToExit.ToList().Contains(chest)))
            {
                return(ConvertToDirections(pathFromStartToExit, true));
            }

            var pathsFromStart = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
            var pathsFromExit  = BfsTask.FindPaths(map, map.Exit, map.Chests);
            var shortestPath   = pathsFromStart
                                 .Join(pathsFromExit, path => path.Value, path => path.Value, ConvertToDirections)
                                 .OrderBy(path => path.Length)
                                 .FirstOrDefault();

            return(shortestPath ?? ConvertToDirections(pathFromStartToExit, true));
        }
        public static MoveDirection[] FindShortestPath(Map map)
        {
            var exit = BfsTask.FindPaths(map, map.InitialPosition, new[] { map.Exit }).FirstOrDefault();

            if (exit == null)
            {
                return(new MoveDirection[0]);
            }

            var pathToExit   = BfsTask.FindPaths(map, map.Exit, map.Chests).DefaultIfEmpty();
            var pathToChests = BfsTask.FindPaths(map, map.InitialPosition, map.Chests);

            var exitList = exit.ToList();

            exitList.Reverse();
            if (pathToChests.FirstOrDefault() == null)
            {
                return(ConvertPointsToDirections(exitList));
            }

            var pathPoints = GetShortestPath(pathToChests, pathToExit);

            return(ConvertPointsToDirections(pathPoints));
        }