Пример #1
0
        static void Explore(Node start, int range)
        {
            HeapMin <Node> openSet   = new HeapMin <Node> ();
            HashSet <Node> closedSet = new HashSet <Node> ();

            openSet.Add(start);

            while (openSet.Count > 0)
            {
                Node current = openSet.Remove();
                closedSet.Add(current);

                if (!(current.gCost < range))
                {
                    continue;
                }

                foreach (Node n in current.GetConnections())
                {
                    if (closedSet.Contains(n) || !n.IsWalkable())
                    {
                        continue;
                    }

                    float newCost = current.gCost + GetDistance(current, n);
                    int   newHops = current.gHops + 1;
                    bool  inOpen  = openSet.Contains(n);
                    if (newCost < n.gCost || !inOpen)
                    {
                        n.gCost   = newCost;
                        n.gHops   = newHops;
                        n.gParent = current;

                        if (!inOpen)
                        {
                            openSet.Add(n);
                        }
                    }
                }
            }
        }
Пример #2
0
        public static List <Node> FindPath(Node start, Node goal)
        {
            HeapMin <Node> openSet   = new HeapMin <Node> ();
            HashSet <Node> closedSet = new HashSet <Node> ();

            openSet.Add(start);

            while (openSet.Count > 0)
            {
                Node current = openSet.Remove();
                closedSet.Add(current);

                if (current == goal)
                {
                    return(RetracePath(start, goal));
                }

                foreach (Node n in current.GetConnections())
                {
                    if (closedSet.Contains(n) || !n.IsWalkable())
                    {
                        continue;
                    }

                    float newCost = current.gCost + GetDistance(current, n);
                    bool  inOpen  = openSet.Contains(n);
                    if (newCost < n.gCost || !inOpen)
                    {
                        n.gCost   = newCost;
                        n.hCost   = GetDistance(n, goal);
                        n.gParent = current;

                        if (!inOpen)
                        {
                            openSet.Add(n);
                        }
                    }
                }
            }
            return(null);
        }