当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.Find方法代码示例

本文整理汇总了C#中PriorityQueue.Find方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.Find方法的具体用法?C# PriorityQueue.Find怎么用?C# PriorityQueue.Find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.Find方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FindPath

    public static Vector3[] FindPath(int[] start, int[] end)
    {
        PathManager mgr = GetInstance ();

        Vector3[] outPath;
        if (mgr.QueryCache (start, end, out outPath)) {
            return outPath;
        }

        int pathSearched = 0;

        GridManager grid = GridManager.GetInstance ();
        if (!grid.HasSurface (start) || !grid.HasSurface (end)) {
            return null;
        }

        List<Node> closed = new List<Node> ();
        PriorityQueue<Node> open = new PriorityQueue<Node> ();
        open.comparator = Node.Comparison;

        Vector3 goal = grid.GridCenter (end);

        Node s = new Node ();
        s.edge = null;
        s.g_score = 0.0f;
        s.h_score = mgr.SqDist (s.p, goal);
        s.f_score = s.h_score;
        s.p = grid.GridCenter (start);

        open.Add (s);

        while (!open.Empty ()) {
            Node node = open.Top;
            open.Remove (open.Top);

            // if it ends, end.
            if (node.p == goal) {
                List<Vector3> path = new List<Vector3> ();
                mgr.ReconstructPath (node, path);
                Vector3[] outpath = path.ToArray ();
                mgr.CachePath (start, end, outpath);
                return outpath;
            }

            closed.Add (node);

            List<Vector3> neighbors = mgr.Neighbors(node.p);
            foreach (Vector3 n in neighbors) {
                if (closed.FindIndex ((a) => a.p == n) >= 0) {
                    continue;
                }
                float temp_g = node.g_score + mgr.SqDist(node.p, n);
                Node f = open.Find ((a) => a.p == n);
                if (f == null) {
                    f = new Node ();
                    f.edge = node;
                    f.g_score = temp_g;
                    f.h_score = mgr.SqDist(n, goal);
                    f.f_score = f.g_score + f.h_score;
                    f.p = n;
                    open.Add (f);
                } else if (temp_g < f.g_score) {
                    f.edge = node;
                    f.g_score = temp_g;
                    f.f_score = f.g_score + f.h_score;
                    open.Update (f);
                }
            }
            if (pathSearched >= mgr.pathSearchLimit) {
                return null;
            } else {
                pathSearched ++;
            }
        }
        return null;
    }
开发者ID:tosos,项目名称:RTSComponents,代码行数:76,代码来源:PathManager.cs


注:本文中的PriorityQueue.Find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。