Ejemplo n.º 1
0
            private Dictionary <Coords, PathData> GetReachableSpots(Coords start)
            {
                var toReturn = new Dictionary <Coords, PathData>();

                // Let's not worry about optimization yet.  The simple solution might be good enough.
                var reached = new Dictionary <string, PathData>();

                var pathData = new PathData();

                reached.Add(start.ToString(), pathData);
                pathData.Push(Direction.Up);
                GetReachableSpotsRecursive(start.Up(), (PathData)pathData.Clone(), reached);
                pathData.Pop();
                pathData.Push(Direction.Left);
                GetReachableSpotsRecursive(start.Left(), (PathData)pathData.Clone(), reached);
                pathData.Pop();
                pathData.Push(Direction.Right);
                GetReachableSpotsRecursive(start.Right(), (PathData)pathData.Clone(), reached);
                pathData.Pop();
                pathData.Push(Direction.Down);
                GetReachableSpotsRecursive(start.Down(), (PathData)pathData.Clone(), reached);
                pathData.Pop();

                foreach (var key in reached.Keys)
                {
                    var keySplit = key.Split(',');
                    toReturn.Add(new Coords {
                        Row = int.Parse(keySplit[0]), Col = int.Parse(keySplit[1])
                    }, reached[key]);
                }

                return(toReturn);
            }
Ejemplo n.º 2
0
            private void GetReachableSpotsRecursive(Coords start, PathData pathSoFar, Dictionary <string, PathData> reached)
            {
                // If this is already in the dictionary, jump out.
                if (reached.Keys.Contains(start.ToString()))
                {
                    if (reached[start.ToString()].CompareTo(pathSoFar) < 0)
                    {
                        return;
                    }
                    else
                    {
                        reached.Remove(start.ToString());
                    }
                }

                reached.Add(start.ToString(), (PathData)pathSoFar.Clone());

                // If we're not on a space, jump out.
                if (GetSpot(start) != Spot.Space)
                {
                    return;
                }

                pathSoFar.Push(Direction.Up);
                GetReachableSpotsRecursive(start.Up(), (PathData)pathSoFar.Clone(), reached);
                pathSoFar.Pop();
                pathSoFar.Push(Direction.Left);
                GetReachableSpotsRecursive(start.Left(), (PathData)pathSoFar.Clone(), reached);
                pathSoFar.Pop();
                pathSoFar.Push(Direction.Right);
                GetReachableSpotsRecursive(start.Right(), (PathData)pathSoFar.Clone(), reached);
                pathSoFar.Pop();
                pathSoFar.Push(Direction.Down);
                GetReachableSpotsRecursive(start.Down(), (PathData)pathSoFar.Clone(), reached);
                pathSoFar.Pop();
            }