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


C# PriorityQueue.ChangePriority方法代码示例

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


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

示例1: FindPath

    public bool FindPath()
    {
        //Check if indexes are valid
        if (!IsIndexValid (First) || !IsIndexValid (Last))
            return false;

        //Check if starting and ending position are same
        if (First == Last) {
            ConstructPath (new Dictionary<Point, Point> (), Last);
            return true;
        }

        var profiler = new Stopwatch ();
        profiler.Start ();

        //Create open/close sets and cameFrom
        var closed = new HashSet<Point> ();
        var open = new PriorityQueue<Point> (true);
        var cameFrom = new Dictionary<Point, Point> ();

        //Create f/g score dicts
        var gScore = new Dictionary<Point, float> ();
        var fScore = new Dictionary<Point, float> ();

        //Prepare the first node
        var current = First;
        gScore [current] = 0f;
        fScore [current] = gScore [current] + CostEstimate (current, Last);
        open.Insert (current, fScore [current]);

        //Main loop
        while (!open.IsEmpty) {
            //Take the first in the priority queue
            current = open.Pop ();
            closed.Add (current);

            //If goal reached
            if (current == Last) {
                ConstructPath (cameFrom, current);
                profiler.Stop ();
                ComputationTime = profiler.Elapsed;
                return true;
            }

            //Iterate neighbours
            var neighbours = GetNeighbours (current);
            foreach (var neighbourPair in neighbours) {
                var neighbour = neighbourPair.Key;
                var cost = neighbourPair.Value;
                //Don't search closed nodes
                if (closed.Contains (neighbour))
                    continue;

                //Check if it's worth it
                var tentativeGScore = gScore [current] + cost;
                if (open.Contains (neighbour) && tentativeGScore >= gScore [neighbour])
                    continue;

                //Set up neighbour
                cameFrom [neighbour] = current;
                gScore [neighbour] = tentativeGScore;
                fScore [neighbour] = gScore [neighbour] + CostEstimate (neighbour, Last);

                //Change priority or add it to the priority queue
                if (open.ChangePriority (neighbour, fScore [neighbour]))
                    continue;

                open.Insert (neighbour, fScore [neighbour]);
            }
        }

        profiler.Stop ();
        ComputationTime = profiler.Elapsed;
        return false;
    }
开发者ID:Botyto,项目名称:UnityTown,代码行数:75,代码来源:IsoPathfinder.cs


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