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


C# PriorityQueue.isEmpty方法代码示例

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


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

示例1: findShortestPath

    //-------------------------------------Movement-----------------------------------------//
    // A* algorithm to find shortest path to desired tile.
    private Path<Tile> findShortestPath(Tile start, Tile end)
    {
        PriorityQueue<int, Path<Tile>> open = new PriorityQueue<int, Path<Tile>>();
        HashSet<Tile> closed = new HashSet<Tile>();
        open.Enqueue(0, new Path<Tile>(start));
        int cost = 1;

        while (!open.isEmpty())
        {
            var path = open.Dequeue();
            if (closed.Contains(path.LastStep))
            {
                continue;
            }
            if (path.LastStep.Equals(end))
            {
                return path;
            }
            closed.Add(path.LastStep);
            foreach (Tile t in path.LastStep.connectedTiles)
            {
                if (t.isBlocked)
                {
                    closed.Add(t);
                    continue;
                }

                var newPath = path.AddStep(t, cost);
                open.Enqueue(newPath.TotalCost, newPath);
            }
        }
        return null;
    }
开发者ID:romanlarionov,项目名称:Sabotage,代码行数:35,代码来源:GameBoardPlayer.cs

示例2: findPath

    //This is the method that will do A*. It returns a vector of locations to follow
    public LinkedList<Vector3> findPath(Vector3 start, Vector3 end)
    {
        LinkedList<Vector3> result = new LinkedList<Vector3> ();
        Node startNode = currentGraph.getNodeByLocation ((int)start.y, (int)start.x);
        startNode.rawCost = 0.0f;
        queue = new PriorityQueue (startNode);
        while (!queue.isEmpty()) {
            //The A* magic happens here
            Node minNode = queue.pop ();
            //if this is our ending node, stop pathfinding and form our full path on the graph
            if (minNode == currentGraph.getNodeByLocation ((int) end.y, (int) end.x)) {
                //Here we form the path depending
                Node currentNode = minNode;

                while (currentNode != null) {
                    result.AddFirst(new Vector3(currentNode.widthPos, currentNode.heightPos));
                    currentNode = currentNode.parent;
                }
                resetGraph();
                return result;
            }
            //else, we need to update our priority queue, etc.
            else {
                float currentRaw = minNode.rawCost;
                foreach (Node neighbor in minNode.edges.Values) {
                    if (queue.isVisited(neighbor)) {
                        float oldRaw = neighbor.rawCost;
                        float newRaw = currentRaw + Vector2.Distance(new Vector2(minNode.widthPos, minNode.heightPos), new Vector2(neighbor.widthPos, neighbor.heightPos));
                        if (newRaw < oldRaw) {
                            neighbor.rawCost = newRaw;
                            neighbor.parent = minNode;
                        }
                    } else {
                        neighbor.rawCost = currentRaw + Vector2.Distance(new Vector2(minNode.widthPos, minNode.heightPos), new Vector2(neighbor.widthPos, neighbor.heightPos));
                        neighbor.parent = minNode;
                        queue.insert(neighbor);
                    }
                }
            }
        }
        resetGraph ();
        return result;
    }
开发者ID:charder,项目名称:GameAIUnity,代码行数:44,代码来源:WorldCreator.cs

示例3: Find

    /// <summary>
    /// Find the vector telling next location to go to.
    /// if fails to find a solution, it returns the target by default
    /// </summary>
    /// <param name='position'>
    /// Position.
    /// </param>
    /// <param name='target'>
    /// Target.
    /// </param>
    /// <param name='mapInfo'>
    /// LevelInfo containing info on the level.
    /// </param>
    public static Vector2 Find(Vector2 position, Vector2 target, LevelInfo mapInfo)
    {
        if (canGoStraightLine(position, target, mapInfo)) return target;

        Vector2 defaultRtrnLoc = target; // by default tell to go straight to target

        if (!mapInfo.inBounds(position)) {
            // this SHOULDN'T happen, you should not be out of bounds of the level
            return defaultRtrnLoc;
        }

        int xPos = (int)position.x;
        int yPos = (int)position.y;
        int xTarget = (int)target.x;
        int yTarget = (int)target.y;

        // use 3th dimension to store c(p) in f(p) = h(p) + c(p)
        PriorityQueue<Vector3> frontier = new PriorityQueue<Vector3>();

        // add start position to frontier
        int cp = 0;
        int fp = getHP(position, target) + cp;
        frontier.enqueue(fp, new Vector3(xPos, yPos, cp));

        // backtracking (cpsc320 anyone?)
        // might not be the optimal method, but it works
        // Vector3, x, y map to coords, z is used for cost
        // z = -1 if visited, non-neg integer if not visited = cost to reach location
        Vector3[,] backtrack = new Vector3[(int)mapInfo.getSize().x, (int)mapInfo.getSize().y];
        // set the backtrack for position to -1,-1
        backtrack[xPos, yPos] = new Vector3(-1, -1, 1);

        for (int i = 0; i < STEP_LIMIT; i++) {
            if (frontier.isEmpty()) {
                return defaultRtrnLoc; // can't reach it...
            }

            // select lowest f(p) = h(p) + c(p)
            Vector3 nextDequeue = frontier.dequeue();
            Vector2 nextLocation = new Vector2(nextDequeue.x, nextDequeue.y);

            // double check that this location has not been visited
            if (backtrack[(int)nextLocation.x, (int)nextLocation.y].z == -1) {
                continue;
            }

            // check if goal
            if ((int)nextLocation.x == xTarget && (int)nextLocation.y == yTarget) {

                // backtrack from this position....
                // find first step to move
                return getBacktrack(backtrack, target, position, mapInfo);
            }
            // mark location as accessed
            backtrack[(int)nextLocation.x, (int)nextLocation.y].z = -1;

            bool verify_diag = false;
            // otherwise add neighbour of those to frontier that:
            for (int deltaX = -1; deltaX <= 1; deltaX++) {
                for (int deltaY = -1; deltaY <=1; deltaY++) {
                    verify_diag = false;
                    if (NO_DIAGONAL_MOVEMENT && (deltaX * deltaY != 0)) {
                        continue;
                    } else {
                        if (deltaX * deltaY != 0) {
                        // verify that we can actually move diagonally
                            verify_diag = true;
                        }
                    }

                    Vector2 neighbour = nextLocation + new Vector2(deltaX, deltaY);

                    int thisCP = (int)nextDequeue.z + Mathf.Abs(deltaX * deltaY) * DIAG_COST
                                    + LINEAR_COST;
                    int thisFP = getHP(neighbour, target) + thisCP;

                    // a) is within boundaries
                    if (!mapInfo.inBounds(neighbour)) { continue; }

                    // b) have not been visited or it's cheaper to go this way
                    Vector3 locInfo = backtrack[(int)neighbour.x, (int)neighbour.y];
                    if (locInfo != Vector3.zero && locInfo.z < thisCP) { continue; }

                    // c) are accessible from this location (walkable as specified by mapInfo)
                    if (mapInfo.isOccupiedBy(neighbour, MapScript.ENEMY_GROUP)) { continue; }
                    if (verify_diag) {
                        if (mapInfo.isOccupiedBy(nextLocation + new Vector2(0, deltaY),
//.........这里部分代码省略.........
开发者ID:hwchan,项目名称:new-kite,代码行数:101,代码来源:AStar.cs

示例4: Dijkstras

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////  Dijktra's Algorithm ////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /**
        * This function will implement Dijkstra's Algorithm to find the shortest path to all the nodes from the source node
        * Time Complexity: O((|V| + |E|) log|V|) with a heap as it iterates over all the nodes and all the edges and uses a queue to go
        * through them where the heap queue has a worst case of log|V|. Whereas if the queue was implemented with an array, the complexity
        * would be O((|V|^2) since the queue has a worst case of |V| and |E| is upper bounded by |V|^2 and so |V|^2 dominates.
        * Space Complexity: O(|V|) as it creates arrays as big as the number of nodes in the graph
        */
        private List<int> Dijkstras(ref PriorityQueue queue, bool isArray)
        {
            // Create Queue to track order of points
            queue.makeQueue(points.Count);
            // Set up prev node list
            List<int> prev = new List<int>();
            List<double> dist = new List<double>();
            for (int i = 0; i < points.Count; i++)
            {
                prev.Add(-1);
                dist.Add(double.MaxValue);
            }

            // Initilize the start node distance to 0
            dist[startNodeIndex] = 0;
            
            // Update Priority Queue to reflect change in start point distance
            if (isArray)
                queue.insert(startNodeIndex, 0);
            else
                queue.insert(ref dist, startNodeIndex);

            // Iterate while the queue is not empty
            while (!queue.isEmpty())
            {
                // Grab the next min cost Point
                int indexOfMin;
                if (isArray)
                    indexOfMin = queue.deleteMin();
                else
                    indexOfMin = queue.deleteMin(ref dist);

                PointF u = points[indexOfMin];
                
                // For all edges coming out of u
                foreach (int targetIndex in adjacencyList[indexOfMin])
                {
                    PointF target = points[targetIndex];
                    double newDist = dist[indexOfMin] + computeDistance(u, target);
                    if (dist[targetIndex] > newDist)
                    {
                        prev[targetIndex] = indexOfMin;
                        dist[targetIndex] = newDist;
                        if (isArray)
                            queue.decreaseKey(targetIndex, newDist);
                        else
                            queue.decreaseKey(ref dist, targetIndex);
                    }
                }
            }
            return prev;
        }
开发者ID:YazanHalawa,项目名称:CS-312,代码行数:66,代码来源:Form1.cs

示例5: Flee

    /***
     * Flee A* implementation
     * - what this does is similiar to A*. It searches and returns the location with the highest "distance"
     *   from the player. It has a limit to the distance it is willing to search (o), and it has a distance
     *   that it (P) refuses to come close to (x) the target (T)
     *
     *   Think of it as a donut shape centered around the target. The width of the donut depends on the distance
     *   between the position and the target
     *    --------------------------
     *    |  ooooooooo              |
     *    | oooooxooooo             |
     *    |ooPooxxxooooo            |
     *    |ooooxxTxxooooo           |
     *    |oooooxxxooooo            |
     *    | oooooxooooo             |
     *    |  ooooooooo              |
     *     -------------------------
     *
     * */
    public static Vector2 Flee(Vector2 position, Vector2 target, LevelInfo mapInfo)
    {
        float minDistancePercentage = 50.0f; //%
        float maxDistancePercentage = 350.0f; //%
        // if position and target are "r" units apart
        // will find cheapest path to farthest location that does not get within r*50% of
        // target, and no farther than r*350% of target
        // (of course, if this is repeatedly called, as you get farther from target
        // your search range increases

        // Alternatively, think of it as how "panic'd" the AI is.
        // if a AI running away from the target is really close
        // it will "panic" and simply tries to get slightly farther away
        // as it reaches farther away, it "relaxes" and makes a better analysis of the
        // possible paths.

        float distance = getDistance(position, target);
        int minDistance = (int)(distance * minDistancePercentage / 100.0f) - 1;
        int maxDistance = (int)(distance * maxDistancePercentage / 100.0f) + 1;

        Vector2 defaultRtrnLoc = position;//position + (position - target); // by default tell to go away

        if (!mapInfo.inBounds(position)) {
            // this SHOULDN'T happen, you should not be out of bounds of the level
            return defaultRtrnLoc;
        }

        int xPos = (int)position.x;
        int yPos = (int)position.y;

        // never go farther than twice the distance (computational resource reasons)

        // use 3rd dimension to store c(p) in f(p) = h(p) + c(p)
        PriorityQueue<Vector3> frontier = new PriorityQueue<Vector3>();

        // farthest location so far
        Vector2 farthestPoint = position;
        int farthestPointFP = int.MaxValue;

        // add start position to frontier
        int cp = 0;
        int fp = getFleeHP(position, target) + cp;
        frontier.enqueue(fp, new Vector3(xPos, yPos, cp));

        // backtracking (cpsc320 anyone?)
        // might not be the optimal method, but it works
        // Vector3, x, y map to coords, z is used for cost
        // z = -1 if visited, non-neg integer if not visited = cost to reach location
        Vector3[,] backtrack = new Vector3[(int)mapInfo.getSize().x, (int)mapInfo.getSize().y];
        // set the backtrack for position to -1,-1
        backtrack[xPos, yPos] = new Vector3(-1, -1, 1);

        for (int i = 0; i < STEP_LIMIT; i++) {
            if (frontier.isEmpty()) {
                //return defaultRtrnLoc; // can't reach it...
                return getBacktrack(backtrack, farthestPoint);
            }

            // select lowest f(p) = h(p) + c(p)
            Vector3 nextDequeue = frontier.dequeue();
            Vector2 nextLocation = new Vector2(nextDequeue.x, nextDequeue.y);

            // double check that this location has not been visited
            if (backtrack[(int)nextLocation.x, (int)nextLocation.y].z == -1) {
                continue;
            }

            // mark location as accessed
            backtrack[(int)nextLocation.x, (int)nextLocation.y].z = -1;

            bool verify_diag = false;
            // otherwise add neighbour of those to frontier that:
            for (int deltaX = -1; deltaX <= 1; deltaX++) {
                for (int deltaY = -1; deltaY <=1; deltaY++) {
                    verify_diag = false;
                    if (NO_DIAGONAL_MOVEMENT && (deltaX * deltaY != 0)) {
                        continue;
                    } else {
                        if (deltaX * deltaY != 0) {
                        // verify that we can actually move diagonally
                            verify_diag = true;
//.........这里部分代码省略.........
开发者ID:hwchan,项目名称:new-kite,代码行数:101,代码来源:AStar.cs


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