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


C# PriorityQueue.enqueueWithPriority方法代码示例

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


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

示例1: TestEnqueueDequeue

        public void TestEnqueueDequeue()
        {
            PriorityQueue<int> queue = new PriorityQueue<int>();
            queue.enqueueWithPriority (16,4);
            queue.enqueueWithPriority (14,5);
            queue.enqueueWithPriority (25,3);
            queue.enqueueWithPriority (17,4);

            Assert.AreEqual (queue.dequeue (), 14);
            Assert.AreEqual (queue.dequeue (), 16);
            Assert.AreEqual (queue.dequeue (), 17);
            Assert.AreEqual (queue.dequeue (), 25);

            queue.enqueueWithPriority(11,1);

            Assert.AreEqual (queue.dequeue (), 11);

            try {
                Console.Out.WriteLine("Attempting to dequeue an empty queue.");
                queue.dequeue();
                Console.Error.WriteLine("FAIL: System.InvalidOperationExcepti"+
                             "on was not thrown when dequeing an empty queue");
                Assert.IsTrue(false);
            } catch (System.InvalidOperationException e)
            {
                Console.Out.WriteLine("PASS: System.InvalidOperationException"+
                                            " thrown as expected: "+e.Message);
                Assert.IsTrue(true,"Exception thrown");
            }
        }
开发者ID:Syclamoth,项目名称:GamesAI6,代码行数:30,代码来源:PriorityQueueTests.cs

示例2: getObjectsObservableBy

    public PriorityQueue<IHearable> getObjectsObservableBy(IHearing listener)
    {
        //Brute force approach = O(n)
        //For all hearing objects it's O(m*n) ~ Acceptable.
        PriorityQueue<IHearable> queue = new PriorityQueue<IHearable>(true);
        int i;

        IHearable target;
        for (i=0;i<hearableObjects.Count;++i)
        {
            double distanceSquared,priority; //Hopefully this will be optimized out.

            target = hearableObjects[i];

            //Calculates Distance^2
            distanceSquared = (listener.getLocation() - target.getLocation()).sqrMagnitude;

            //Store in variable for use as queue priority
            priority = target.getVolume().volumeFromDistanceSquared(distanceSquared).Intensity;

            //Put in queue if louder than hearing threshold.
            //Debug.Log(Volume.fromIntensity(priority).Decibels + " " + target.getGameObject().name);
            if (priority >= listener.getHearingThreshold().Intensity)
            {
                //Debug.Log(Volume.fromIntensity(priority).Decibels + " " + target.getGameObject().name);
                queue.enqueueWithPriority(target,priority);
            }
        }

        return queue;
    }
开发者ID:Syclamoth,项目名称:GamesAI6,代码行数:31,代码来源:SoundManager.cs

示例3: findPath

    /*!!! PATHFINDING ALGORITHM IS HIDDEN HERE !!!*/
    public LinkedList<GridSquare> findPath(Vector3 pathFrom,Vector3 pathTo)
    {
        AStarNode[] aStarNodes = new AStarNode[width*height];
        bool[] closedSet = new bool[width*height];
        PriorityQueue<GridSquare> openSet = new PriorityQueue<GridSquare>(false);

        GridSquare target = this.gridSquareFromVector3(pathTo);
        GridSquare currentSquare;

        AStarNode current;

        //Initialize with path beginning
        AStarNode temp = new AStarNode(this.gridSquareFromVector3(pathFrom),target);
        aStarNodes[temp.getSquare().getHash()] = temp;
        openSet.enqueueWithPriority(temp.getSquare(),temp.getFScore());

        //While there's still items in the open set
        while ((currentSquare = openSet.dequeue()) != null) {
            //openSet stores gridsquares for efficiency reasons, so get the relevant A* node
            current = aStarNodes[currentSquare.getHash()];

            //Add node to the closed set
            closedSet[current.getSquare().getHash()] = true;

            //If the current square is the target, we have found a path and
            //can return
            if (current.getSquare () == target) {
                break;
            }

            //For every neighbor
            foreach (GridSquare s in current.getNeighbors())
            {
                //If the square is already processed, skip it
                if (closedSet[s.getHash()] == true) {
                    continue;
                }
                //This is why the open set stores GridSquares instead of AStarNodes
                if (!openSet.Contains(s)) {
                    temp = new AStarNode(s,target);
                    aStarNodes[temp.getSquare().getHash()] = temp;

                    //setParent sets the g score automatically.
                    temp.setParent(current);

                    openSet.enqueueWithPriority(temp.getSquare(),temp.getFScore());
                } else {
                    //if this is a worse path, skip it.
                    temp = aStarNodes[s.getHash ()];
                    if (current.gScore + 1 >= temp.gScore) {
                        continue;
                    }
                    //setParent sets the g score automatically.
                    temp.setParent(current);

                    //Re add to the open set.
                    openSet.Remove (temp.getSquare());
                    openSet.enqueueWithPriority(temp.getSquare(),temp.getFScore());
                }

            }
        }

        //No path was found
        if (currentSquare == null) {
            return default(LinkedList<GridSquare>);
        }

        //Reconstruct the path
        LinkedList<GridSquare> path = new LinkedList<GridSquare>();
        current = aStarNodes[target.getHash()];

        if (current == null) {
            return null;
        }

        do {
            //Add the current square to the beginning of the path
            path.AddFirst(current.getSquare());
            //Set the current node to the parent
            current = current.getParent ();
        } while (current != null);

        return path;
    }
开发者ID:Syclamoth,项目名称:GamesAI6,代码行数:86,代码来源:Grid.cs


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