コード例 #1
0
        public Distances Distances()
        {
            var distances = new Distances(this);
            var frontier  = new List <Cell> {
                this
            };

            while (frontier.Any())
            {
                var newFrontier = new List <Cell>();

                foreach (var cell in frontier)
                {
                    foreach (var linked in cell.Links())
                    {
                        if (distances.ContainsKey(linked))
                        {
                            continue;
                        }
                        distances.Add(linked, distances[cell] + 1);
                        newFrontier.Add(linked);
                    }
                }
                frontier = newFrontier;
            }

            return(distances);
        }
コード例 #2
0
        public Distances PathTo(Cell goal)
        {
            Cell current = goal;

            Distances breadcrumbs = new Distances(root);

            breadcrumbs[current] = this[current];

            while (current != root)
            {
                foreach (var neighbor in current.Links())
                {
                    if (this[neighbor] < this[current])
                    {
                        if (!breadcrumbs.ContainsKey(neighbor))
                        {
                            breadcrumbs.Add(neighbor, this[neighbor]);
                        }
                        else
                        {
                            breadcrumbs[neighbor] = this[neighbor];
                        }

                        current = neighbor;
                        break;
                    }
                }
            }

            return(breadcrumbs);
        }
コード例 #3
0
        public Distances PathTo(Cell goal)
        {
            var current = goal;

            var breadcrumbs = new Distances(_root)
            {
                [current] = this[current]
            };

            while (current != _root)
            {
                foreach (var neighbour in current.Links())
                {
                    if (this[neighbour] < this[current])
                    {
                        if (!breadcrumbs.ContainsKey(neighbour))
                        {
                            breadcrumbs.Add(neighbour, this[neighbour]);
                        }
                        else
                        {
                            breadcrumbs[neighbour] = this[neighbour];
                        }
                        current = neighbour;
                        break;
                    }
                }
            }

            return(breadcrumbs);
        }
コード例 #4
0
        public override string ContentsOf(Cell cell)
        {
            if (Distances != null && Distances.ContainsKey(cell))
            {
                return(Base36.Encode(Distances[cell]));
            }

            return(" ");
        }