Beispiel #1
0
        /// <summary>
        /// Gets all keys that lie along the given path. Does not include the
        /// starting point.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public HashSet <string> GetKeysAlongPath(IList <GridPoint> path)
        {
            var result = new HashSet <string>();

            for (int i = 0; i < path.Count; i++)
            {
                var point = path[i];
                if (CellsWithKeys.ContainsKey(point))
                {
                    result.Add(CellsWithKeys[point]);
                }
            }
            return(result);
        }
Beispiel #2
0
        private void ConstructMazeGraph()
        {
            var graphNodes = CellsWithDoors.Select(kvp => kvp.Key)
                             .ToList();

            graphNodes.AddRange(CellsWithKeys.Select(kvp => kvp.Key)
                                .ToList());
            graphNodes.AddRange(StartingPositions);
            EdgeShortestPaths = new Dictionary <Tuple <GridPoint, GridPoint>, IList <GridPoint> >();
            EdgeKeys          = new Dictionary <Tuple <GridPoint, GridPoint>, HashSet <string> >();
            IList <GridPoint> GetShortestPathBetweenPoints(GridPoint p1, GridPoint p2)
            {
                var pathResult = GetPathToPoint(
                    startPoint: p1,
                    targetPoint: p2,
                    keysCollected: new SortedDictionary <string, string>(),
                    forceAllowToEnterTarget: true);

                if (pathResult.Path.Count > 0)
                {
                    var edgeKey12 = new Tuple <GridPoint, GridPoint>(p1, p2);
                    var edgeKey21 = new Tuple <GridPoint, GridPoint>(p2, p1);
                    HashSet <string> keysAlongEdge = null;
                    if (!EdgeShortestPaths.ContainsKey(edgeKey12))
                    {
                        keysAlongEdge = GetKeysAlongPath(pathResult.Path);
                        EdgeShortestPaths.Add(edgeKey12, pathResult.Path);
                        EdgeKeys.Add(edgeKey12, keysAlongEdge);
                    }
                    if (!EdgeShortestPaths.ContainsKey(edgeKey21))
                    {
                        var reversedPath = pathResult.Path.ToList();
                        reversedPath.Reverse();
                        keysAlongEdge = GetKeysAlongPath(reversedPath);
                        EdgeShortestPaths.Add(edgeKey21, reversedPath);
                        EdgeKeys.Add(edgeKey21, keysAlongEdge);
                    }
                }
                return(pathResult.Path);
            }

            MazeGraph = new Graph <GridPoint>(graphNodes, GetShortestPathBetweenPoints);
        }