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


C# PriorityQueue.Update方法代码示例

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


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

示例1: using_Dijkstra_s_algorithm_with_priority_queue

        /// <summary>
        /// based on the pseudocode on wiki page(http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
        /// </summary>
        static int using_Dijkstra_s_algorithm_with_priority_queue()
        {
            var len = 80;
            var node_map = new Node[len, len];
            var queue = new PriorityQueue(len * len);
            Node source = null;
            Node goal = null;
            var row = 0;
            var col = 0;
            foreach (var line in File.ReadAllLines("matrix.txt"))
            {
                col = 0;
                foreach (var num in line.Split(new char[] { ',' }))
                {
                    var node = new Node(Convert.ToInt32(num)) { row = row, col = col };
                    if (row == 0 && col == 0) source = node;
                    if (row == len - 1 && col == len - 1) goal = node;
                    // node map is mainly used to get neighbors
                    node_map[row, col] = node;
                    queue.Enqueue(node);
                    col++;
                }
                row++;
            }
            // set the source's distance to zero to kick start the process
            queue.Update(source, 0);
            // code for getting neighbor, using closure with the neighbor_list to make life a little easier
            var neighbor_list = new Node[4]; // 0:left 1:up 2:right 3:down
            Action<Node> prepare_neighbor_list = n =>
                {
                    neighbor_list[0] = n.col - 1 < 0 ? null : node_map[n.row, n.col - 1];
                    neighbor_list[1] = n.row - 1 < 0 ? null : node_map[n.row - 1, n.col];
                    neighbor_list[2] = n.col + 1 >= len ? null : node_map[n.row, n.col + 1];
                    neighbor_list[3] = n.row + 1 >= len ? null : node_map[n.row + 1, n.col];
                };
            var total = 0;
            while (queue.IsEmpty() == false)
            {
                var u = queue.DequeueMin();
                if (u.distance == int.MaxValue)
                    break; // all remaining vertices are inaccessible from source
                if (u == goal)
                {
                    while (u != null)
                    {
                        total += u.cost;
                        u = u.previous;
                    }
                    break;
                }
                // call this method before using neighbor_list array
                prepare_neighbor_list(u);
                foreach (var v in neighbor_list)
                {
                    if (v == null)
                        continue; // like when u is edge cell in the matrix

                    var alt = u.distance + u.cost + v.cost;
                    if (alt < v.distance)
                    {
                        v.previous = u;
                        queue.Update(v, alt);
                    }
                }
            }
            return total;
        }
开发者ID:jtg2078,项目名称:ProjectEuler,代码行数:70,代码来源:Program.cs

示例2: patch_type_decompose

        private void patch_type_decompose()
        {
            int n = mesh.FaceCount;
            for (int i = 0; i < n; ++i) // -- reset the labels --
                fnode[i].label = -1;
            // -- label seed faces --
            bool[] visited = new bool[n];
            bool[] labeled = new bool[n];
            if (patchid == null) patchid = new byte[n];
            List<int> sources = new List<int>();
            List<int> targets = new List<int>();
            foreach (List<int> flist in this.facesOnStrokes)
            {
                if (flist.Count < 1) continue;
                sources.Add(flist[0]);
                targets.Add(flist[flist.Count - 1]);
            }
            byte pid = patchid[sources[0]]; // -- the patch strokes lies on --
            foreach (int f in sources)
            {
                fnode[f].label = 0;
                visited[f] = true;
                labeled[f] = true;
            }
            int label = 1;
            foreach (int f in targets)
            {
                fnode[f].label = label;
                visited[f] = true;
                labeled[f] = true;
            }
            // -- region growing --
            PriorityQueue iQue = new PriorityQueue();
            foreach (int f in sources)
            {
                foreach (int adj in mesh.AdjFF[f])
                {
                    if (patchid[adj] != pid) continue;
                    fnode[adj].label = fnode[f].label;
                    fnode[adj].dis = Face_wij(f, adj);
                    iQue.Insert(fnode[adj]);
                    visited[adj] = true;
                }
            }
            foreach (int f in targets)
            {
                foreach (int adj in mesh.AdjFF[f])
                {
                    if (patchid[adj] != pid) continue;
                    fnode[adj].label = fnode[f].label;
                    fnode[adj].dis = Face_wij(f, adj);
                    iQue.Insert(fnode[adj]);
                    visited[adj] = true;
                }
            }
            while (!iQue.IsEmpty())
            {
                GraphNode node = iQue.DeleteMin() as GraphNode;

                int f = node.index;
                labeled[f] = true;
                foreach (int adj in mesh.AdjFF[f])
                {
                    if (patchid[adj] != pid) continue;
                    if (labeled[adj])
                    {
                        continue;
                    }
                    else if (visited[adj])
                    {
                        double wij = Face_wij(f, adj);
                        if (fnode[adj].dis > wij)
                        {
                            fnode[adj].label = fnode[f].label;
                            fnode[adj].dis = wij;
                            iQue.Update(fnode[adj]);
                        }
                    }
                    else
                    {
                        fnode[adj].label = fnode[f].label;
                        fnode[adj].dis = Face_wij(f, adj);
                        iQue.Insert(fnode[adj]);
                        visited[adj] = true;
                    }
                }
            }
            // -- patch the results --
            int index = this.all_patches.Count+1;
            Patch p = new Patch(index);
            p.prev_label = (int)pid;
            for (int i = 0; i < n; ++i)
            {
                if (fnode[i].label == 0)
                {
                    patchid[i] = (byte)index;
                    p.faces.Add(i);
                }
            }
            this.patches.Add(p);
//.........这里部分代码省略.........
开发者ID:GYingchao,项目名称:interactive_cross_boundary_segmentation,代码行数:101,代码来源:HarmonicSolver.cs

示例3: CGI_Cut

 private void CGI_Cut()
 {
     // -- mark verteices on strokes --
     int n = mesh.VertexCount, label = 0;
     bool[] labeled = new bool[n];
     bool[] tag = new bool[n];
     foreach (int v in this.sourceVertices)
     {
         vnode[v].label = label;
         labeled[v] = true;
         tag[v] = true;
     }
     label = 1;
     foreach (int v in this.sinkVertices)
     {
         vnode[v].label = label;
         labeled[v] = true;
         tag[v] = true;
     }
     // -- push to priority queue --
     PriorityQueue iQue = new PriorityQueue();
     foreach (int v in this.sourceVertices)
     {
         foreach (int adj in mesh.AdjVV[v])
         {
             GraphNode node = vnode[adj];
             node.label = vnode[v].label;
             node.dis = Vrt_wij(v, adj);
             tag[adj] = true;
             iQue.Insert(node);
         }
     }
     foreach (int v in this.sinkVertices)
     {
         foreach (int adj in mesh.AdjVV[v])
         {
             GraphNode node = vnode[adj];
             node.label = vnode[v].label;
             node.dis = Vrt_wij(v, adj);
             tag[adj] = true;
             iQue.Insert(node);
         }
     }
     // -- region growing --
     while (!iQue.IsEmpty())
     {
         GraphNode node = iQue.DeleteMin() as GraphNode;
         int v = node.index;
         labeled[v] = true;
         foreach (int adj in mesh.AdjVV[v])
         {
             if (labeled[adj])  // -- already labeled --
             {
                 continue;
             }
             else
             {
                 double cost = Vrt_wij(v, adj);
                 GraphNode adjNode = vnode[adj];
                 adjNode.label = node.label;
                 if (tag[adj]) // -- already in the queue --
                 {
                     if (adjNode.dis > cost)
                     {
                         adjNode.dis = cost;
                         iQue.Update(adjNode);
                     }
                 }
                 else // -- a fresh vertex --
                 {
                     adjNode.dis = cost;
                     tag[adj] = true;
                     iQue.Insert(adjNode);
                 }
             }
         }
     }
     // -- convert to facets --
     List<int> risidual = new List<int>();
     for (int i = 0, j = 0; i < mesh.FaceCount; ++i, j+=3)
     {
         int c0 = mesh.FaceIndex[j];
         int c1 = mesh.FaceIndex[j+1];
         int c2 = mesh.FaceIndex[j+2];
         if (vnode[c0].label == vnode[c1].label &&
             vnode[c0].label == vnode[c2].label)
         {
             fnode[i].label = vnode[c0].label;
         }
         else
         {
             fnode[i].label = -1;
             risidual.Add(i);
         }
     }
     // -- deal with boundary faces --
     while (risidual.Count > 0)
     {
         List<int> vlist = new List<int>();
         vlist.AddRange(risidual);
//.........这里部分代码省略.........
开发者ID:GYingchao,项目名称:interactive_cross_boundary_segmentation,代码行数:101,代码来源:HarmonicSolver.cs

示例4: CalculatePath

        /// <summary>
        /// Calculates a path from start to end. When no path can be found in
        /// reasonable time the search is aborted and an incomplete path is returned.
        /// When refresh is not set to true a cached path is returned where possible.
        /// </summary>
        /// <param name="start">start position in 2d map space</param>
        /// <param name="end">end position in 2d map space</param>
        /// <param name="refresh">force to recalculate the path</param>
        /// <returns></returns>
        public Path CalculatePath(Unit unit, HexPoint start, HexPoint end, bool refresh = false)
        {
            // swap points to calculate the path backwards (from end to start)
            HexPoint temp = end;
            end = start;
            start = temp;

            // Check whether the requested path is already known
            PathRequest request = new PathRequest(unit,start, end);

            if (!refresh && knownPaths.ContainsKey(request))
            {
                return knownPaths[request].Copy();
            }

            // priority queue of nodes that yet have to be explored sorted in
            // ascending order by node costs (F)
            PriorityQueue<PathNode> open = new PriorityQueue<PathNode>();

            // list of nodes that have already been explored
            LinkedList<IGridCell> closed = new LinkedList<IGridCell>();

            // start is to be explored first
            PathNode startNode = new PathNode(unit,null, map[start], end);
            open.Enqueue(startNode);

            int steps = 0;
            PathNode current;

            do
            {
                // abort if calculation is too expensive
                if (++steps > stepLimit) return null;

                // examine the cheapest node among the yet to be explored
                current = open.Dequeue();

                // Finish?
                if (current.Cell.Matches(end))
                {
                    // paths which lead to the requested goal are cached for reuse
                    Path path = new Path(current);

                    if (knownPaths.ContainsKey(request))
                    {
                        knownPaths[request] = path.Copy();
                    }
                    else
                    {
                        knownPaths.Add(request, path.Copy());
                    }

                    return path;
                }

                // Explore all neighbours of the current cell
                ICollection<IGridCell> neighbours = map.GetNeighbourCells(current.Cell);

                foreach (IGridCell cell in neighbours)
                {
                    // discard nodes that are not of interest
                    if (closed.Contains(cell) || (cell.Matches(end) == false && !cell.IsWalkable(unit)))
                    {
                        continue;
                    }

                    // successor is one of current's neighbours
                    PathNode successor = new PathNode(unit,current, cell, end);
                    PathNode contained = open.Find(successor);

                    if (contained != null && successor.F >= contained.F)
                    {
                        // This cell is already in the open list represented by
                        // another node that is cheaper
                        continue;
                    }
                    else if (contained != null && successor.F < contained.F)
                    {
                        // This cell is already in the open list but on a more expensive
                        // path -> "integrate" the node into the current path
                        contained.Predecessor = current;
                        contained.Update();
                        open.Update(contained);
                    }
                    else
                    {
                        // The cell is not in the open list and therefore still has to
                        // be explored
                        open.Enqueue(successor);
                    }
                }
//.........这里部分代码省略.........
开发者ID:mrommel,项目名称:MiRo.SimHexWorld,代码行数:101,代码来源:Pathfinder.cs


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