Exemplo n.º 1
0
        public Distances PathTo(Cell goal)
        {
            Cell current = goal;

            var 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);
        }
Exemplo n.º 2
0
        public Distances GetDistances()
        {
            var         distances = new Distances(this);
            List <Cell> frontier  = new List <Cell>()
            {
                this
            };

            while (frontier.Any())
            {
                List <Cell> new_frontier = 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);
                        new_frontier.Add(linked);
                    }
                }
                frontier = new_frontier;
            }
            return(distances);
        }