Example #1
0
        public void BFS(int node)
        {
            var queue = new Queue <int>();

            queue.Enqueue(node);
            VisitedList.Add(node);
            while (queue.Peek() > 0)
            {
                var pop = queue.Dequeue();
                foreach (var neighbor in AdjacencyList[node])
                {
                    queue.Enqueue(neighbor);
                }
            }
        }
Example #2
0
        public void AStar(MazePointer start, MazePointer end)
        {
            MazePointer initPointer(int x, int y)
            {
                return(new MazePointer()
                {
                    X = x,
                    Y = y,
                    Father = start,
                    W = GetW(end.X - x, end.Y - y),
                    H = start.H + 1
                });
            }

            if (start == null || start.Equals(end))
            {
                return;
            }
            VisitedList.Add(start);
            BackUpList.Remove(start);
            //找到strat周围的点
            MazePointer current = null, top = null, bottom = null, left = null, right = null;

            if (start.Y - 1 >= 0)
            {
                if (MazeGraph[start.X, start.Y - 1] != null)
                {
                    top = initPointer(start.X, start.Y - 1);
                    if (VisitedList.Exists(x => x.X == top.X && x.Y == top.Y))
                    {
                        top = null;
                    }
                    if (top != null)
                    {
                        BackUpList.Add(top);
                    }
                    current = top;
                }
            }
            if (start.Y + 1 < MazeGraph.GetLength(1))
            {
                if (MazeGraph[start.X, start.Y + 1] != null)
                {
                    bottom = initPointer(start.X, start.Y + 1);
                    if (VisitedList.Exists(x => x.X == bottom.X && x.Y == bottom.Y))
                    {
                        bottom = null;
                    }
                    if (bottom != null)
                    {
                        BackUpList.Add(bottom);
                    }
                    current = GetShorter(current, bottom);
                }
            }
            if (start.X - 1 >= 0)
            {
                if (MazeGraph[start.X - 1, start.Y] != null)
                {
                    left = initPointer(start.X - 1, start.Y);
                    if (VisitedList.Exists(x => x.X == left.X && x.Y == left.Y))
                    {
                        left = null;
                    }
                    if (left != null)
                    {
                        BackUpList.Add(left);
                    }
                    current = GetShorter(current, left);
                }
            }
            if (start.X + 1 < MazeGraph.GetLength(0))
            {
                if (MazeGraph[start.X + 1, start.Y] != null)
                {
                    right = initPointer(start.X + 1, start.Y);
                    if (VisitedList.Exists(x => x.X == right.X && x.Y == right.Y))
                    {
                        right = null;
                    }
                    if (right != null)
                    {
                        BackUpList.Add(right);
                    }
                    current = GetShorter(current, right);
                }
            }
            foreach (var backup in BackUpList)
            {
                current = GetShorter(current, backup);
            }
            AStar(current, end);
        }