本文整理汇总了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));
}