Beispiel #1
0
        public static List <Cell> GetPath(Cell from, Cell to, Func <Cell, bool> obstacleCondition)
        {
            if (obstacleCondition(to))
            {
                return(null);
            }

            var fromCell = new BfsCell(null, from);
            var open     = new List <BfsCell> {
                fromCell
            };
            var seen = new HashSet <Cell> {
                from
            };

            while (open.Any())
            {
                var currentCell = open.First();
                open.RemoveAt(0);

                if (currentCell.Cell.Equals(to))
                {
                    var list = new List <Cell>();
                    while (!currentCell.Cell.Equals(from))
                    {
                        list.Insert(0, currentCell.Cell);
                        currentCell = currentCell.Parent;
                    }

                    return(list);
                }

                foreach (var(_, neighbour) in currentCell.Cell.Neighbours)
                {
                    if (seen.Add(neighbour) && !obstacleCondition(neighbour))
                    {
                        open.Add(new BfsCell(currentCell, neighbour));
                    }
                }
            }

            return(null);
        }
Beispiel #2
0
 public BfsCell(BfsCell parent, Cell cell)
 {
     Parent = parent;
     Cell   = cell;
 }