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


C# PriorityQueue.Contain方法代码示例

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


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

示例1: Main

        public static void Main()
        {
            int rows = int.Parse(Console.ReadLine());
            int cols = int.Parse(Console.ReadLine());
            var matrix = new Cell[rows, cols];
            for (int i = 0; i < rows; i++)
            {
                int[] currentRow = Console.ReadLine().Split().Select(int.Parse).ToArray();
                for (int j = 0; j < cols; j++)
                {
                    matrix[i, j] = new Cell(i, j, currentRow[j]);
                }
            }

            matrix[0, 0].DijkstraDistance = 0;
            var queue = new PriorityQueue<Cell>();
            queue.Enqueue(matrix[0, 0]);
            while (queue.Count > 0)
            {
                var cell = queue.ExtractMin();
                int row = cell.Row;
                int col = cell.Col;

                if (IsValidCell(row, col + 1, rows, cols))
                {
                    var rightCell = matrix[row, col + 1];
                    if (rightCell.DijkstraDistance > cell.DijkstraDistance + rightCell.Value)
                    {
                        rightCell.DijkstraDistance = cell.DijkstraDistance + rightCell.Value;
                        rightCell.PreviousCell = cell;
                        if (queue.Contain(rightCell))
                        {
                            queue.DecreaseKey(rightCell);
                        }
                    }

                     if(!rightCell.IsVisited)
                    {
                        queue.Enqueue(rightCell);
                        rightCell.IsVisited = true;
                    }
                }

                if (IsValidCell(row + 1, col, rows, cols))
                {
                    var bottomCell = matrix[row + 1, col];
                    if (bottomCell.DijkstraDistance > cell.DijkstraDistance + bottomCell.Value)
                    {
                        bottomCell.DijkstraDistance = cell.DijkstraDistance + bottomCell.Value;
                        bottomCell.PreviousCell = cell;
                        if (queue.Contain(bottomCell))
                        {
                            queue.DecreaseKey(bottomCell);
                        }
                    }

                    if(!bottomCell.IsVisited)
                    {
                        queue.Enqueue(bottomCell);
                        bottomCell.IsVisited = true;
                    }
                }

                if (IsValidCell(row, col - 1, rows, cols))
                {
                    var leftCell = matrix[row, col - 1];
                    if (leftCell.DijkstraDistance > cell.DijkstraDistance + leftCell.Value)
                    {
                        leftCell.DijkstraDistance = cell.DijkstraDistance + leftCell.Value;
                        leftCell.PreviousCell = cell;
                        if (queue.Contain(leftCell))
                        {
                            queue.DecreaseKey(leftCell);
                        }
                    }

                    if(!leftCell.IsVisited)
                    {
                        queue.Enqueue(leftCell);
                        leftCell.IsVisited = true;
                    }
                }

                if (cell.Row == rows - 1 && cell.Col == cols -1)
                {
                    break;
                }
            }

            var lastCell = matrix[rows - 1, cols - 1];
            var path = new Stack<int>();
            while (lastCell != null)
            {
                path.Push(lastCell.Value);
                lastCell = lastCell.PreviousCell;
            }

            Console.WriteLine("Length: " + path.Sum());
            Console.WriteLine("Path: " + string.Join(" ", path));
        }
开发者ID:pavelilchev,项目名称:HomeWorks,代码行数:100,代码来源:ShortestPathInMatrix.cs


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