本文整理汇总了C#中Heap.Push方法的典型用法代码示例。如果您正苦于以下问题:C# Heap.Push方法的具体用法?C# Heap.Push怎么用?C# Heap.Push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Heap
的用法示例。
在下文中一共展示了Heap.Push方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Dijkstra
public static void Dijkstra(List<Tuple<int, int>>[] adj, int source, out int[] dist, out int[] pred)
{
int inf = int.MaxValue;
int N = adj.Length;
dist = new int[N];
pred = new int[N];
for (int i = 0; i < N; i++)
dist[i] = inf;
dist[source] = 0;
Heap<int, int> heap = new Heap<int, int>(N, true);
heap.Push(source, 0);
while (!heap.IsEmpty())
{
int u = heap.PeekData();
if (dist[u] != heap.Pop().Priority) continue;
foreach (var tuple in adj[u])
{
int v = tuple.Item1;
int uvWeight = tuple.Item2;
if (dist[v] > dist[u] + uvWeight)
{
dist[v] = dist[u] + uvWeight;
pred[v] = u;
heap.Push(v, dist[v]);
}
}
}
}
示例2: Main
static void Main(string[] args)
{
int A = 0;
Console.Write("Podaj ilosc liczb A: ");
A = int.Parse(Console.ReadLine());
Console.Write("Podaj max wartosc M: ");
int M = int.Parse(Console.ReadLine());
Heap<int> heap = new Heap<int>(A);
TList<int> list = new TList<int>();
Console.Write("Podaj ilosc liczb B: ");
int B = int.Parse(Console.ReadLine());
Console.Write("Podaj max wartosc N: ");
int N = int.Parse(Console.ReadLine());
Random r = new Random(DateTime.Now.Millisecond);
Console.WriteLine("Kopiec :)");
var time = Stopwatch.StartNew();
for(var i=A;i>0;i--)
{
var tmp = r.Next(0, M);
heap.Push(tmp,tmp);
}
for(var i= B;i>0;i--)
{
var tmp = r.Next(0, N);
heap.DeleteMax();
heap.Push(tmp,tmp);
}
time.Stop();
var timeMs = time.ElapsedMilliseconds;
Console.WriteLine("Uplynelo {0} ms", timeMs);
Console.ReadLine();
Console.WriteLine("Lista :(");
time = Stopwatch.StartNew();
for (var i = A; i > 0; i--)
{
var tmp = r.Next(0, M);
list.Push(tmp,tmp);
}
for (var i = B; i > 0; i--)
{
list.DeleteAt(list.MinIndex());
var tmp = r.Next(0, N);
list.Push(tmp, tmp);
}
time.Stop();
timeMs = time.ElapsedMilliseconds;
Console.WriteLine("Uplynelo {0} ms", timeMs);
Console.ReadLine();
}
示例3: Main
static void Main(string[] args)
{
Heap test = new Heap();
test.Push(120);
test.Push(230);
test.Push(610);
test.Push(20);
test.Push(620);
//test.Push(4);
//test.Push(50);
Console.WriteLine(test.Top());
test.Pop();
Console.WriteLine(test.Top());
Console.ReadKey();
}
示例4: FindPath
//.........这里部分代码省略.........
GoalNode = new AStarNode(this, null, goalPolygon, 0);
StartNode = new AStarNode(this, null, startPolygon, 0);
Heap openList = new Heap();
Heap closedList = new Heap();
List<AStarNode> solution = new List<AStarNode>();
List<AStarNode> successors = new List<AStarNode>();
int printed = 0;
openList.Add(StartNode);
while (openList.Count > 0) {
//Debug.Log("AStar:main loop");
// Get the node with the lowest TotalSum
AStarNode nodeCurrent = (AStarNode)openList.Pop();
if (printed < 1000){
//Debug.Log("Current polygon: " + nodeCurrent.NavmeshPolygon.name + " - " + nodeCurrent.TotalCost);
printed++;
}
// If the node is the goal copy the path to the solution array
if (GoalNode.Equals(nodeCurrent)) {
//Debug.Log("AStar:finish loop");
while (nodeCurrent != null) {
solution.Insert(0, nodeCurrent);
nodeCurrent = nodeCurrent.Parent;
}
//convert solution
//Debug.Log("Path found");
return solution.ConvertAll(an => an.NavmeshPolygon);
}
// Get successors to the current node
successors.Clear();
nodeCurrent.GetSuccessors(successors);
foreach (AStarNode nodeSuccessor in successors) {
//Debug.Log("AStar:successor loop");
// Test if the currect successor node is on the open list, if it is and
// the TotalSum is higher, we will throw away the current successor.
//Debug.Log("AStarNode nodeOpen");
AStarNode nodeOpen;
if (openList.Contains(nodeSuccessor)) {
//Debug.Log("openList check: nodeSuccessor (" + nodeSuccessor.NavmeshPolygon.name + ") - " + nodeSuccessor.TotalCost);
//Debug.Log("openList contains nodeSuccessor");
//Debug.Log("nodeOpen = (AStarNode)openList[openList.IndexOf(nodeSuccessor)];");
nodeOpen = (AStarNode)openList.GetEqual(nodeSuccessor);
//Debug.Log("openList check: nodeOpen (" + nodeOpen.NavmeshPolygon.name + ") - " + nodeOpen.TotalCost);
//Debug.Log("if ((nodeOpen != null) && (nodeSuccessor.TotalCost > nodeOpen.TotalCost))");
if ((nodeOpen != null) && (nodeSuccessor.TotalCost > nodeOpen.TotalCost)){
//Debug.Log("continue;");
//Debug.Log("openList check: continued");
continue;
} else {
//Debug.Log("openList check: not continued");
}
}
// Test if the currect successor node is on the closed list, if it is and
// the TotalSum is higher, we will throw away the current successor.
//Debug.Log("AStarNode nodeClosed;");
AStarNode nodeClosed;
//Debug.Log("if (closedList.Contains(nodeSuccessor)) {");
if (closedList.Contains(nodeSuccessor)) {
//Debug.Log("closedList check: nodeSuccessor (" + nodeSuccessor.NavmeshPolygon.name + ") - " + nodeSuccessor.TotalCost);
//Debug.Log("closedList contains nodeSuccessor");
//Debug.Log("nodeClosed = (AStarNode)closedList[closedList.IndexOf(nodeSuccessor)];");
nodeClosed = (AStarNode)closedList.GetEqual(nodeSuccessor);
//Debug.Log("closedList check: nodeClosed (" + nodeClosed.NavmeshPolygon.name + ") - " + nodeClosed.TotalCost);
//Debug.Log("if ((nodeClosed != null) && (nodeSuccessor.TotalCost > nodeClosed.TotalCost))");
if ((nodeClosed != null) && (nodeSuccessor.TotalCost > nodeClosed.TotalCost)){
//Debug.Log("continue;");
continue;
}
}
// Remove the old successor from the open list
//Debug.Log("openList.Remove(nodeSuccessor);");
openList.Remove(nodeSuccessor);
// Remove the old successor from the closed list
//Debug.Log("closedList.Remove(nodeSuccessor);");
closedList.Remove(nodeSuccessor);
// Add the current successor to the open list
//Debug.Log("penList.Push(nodeSuccessor);");
if (printed < 1000){
//Debug.Log("Adding to openList: " + nodeSuccessor.NavmeshPolygon.name + " - " + nodeSuccessor.TotalCost);
}
openList.Push(nodeSuccessor);
//Debug.Log("AStar:successor loop finished");
}
// Add the current node to the closed list
//Debug.Log("closedList.Add(nodeCurrent);");
closedList.Add(nodeCurrent);
//Debug.Log("AStar:main loop finished");
}
Debug.Log("AStart.FindPath() Path not found.");
return null;
}
示例5: FindPath
IEnumerator FindPath(Vector3 startPos, Vector3 targetPos)
{
Node startNode = NodeGrid.NodeFromWorldPoint(startPos);
Node targetNode = NodeGrid.NodeFromWorldPoint(targetPos);
Vector3[] waypoints = new Vector3[0];
bool pathSuccess = false;
if (startNode.Walkable && targetNode.Walkable)
{
Heap<Node> openSet = new Heap<Node>(NodeGrid.MaxSize);
HashSet<Node> closedSet = new HashSet<Node>();
openSet.Push(startNode);
while (openSet.Count > 0)
{
Node currentNode = openSet.Pop();
closedSet.Add(currentNode);
if (currentNode == targetNode)
{
pathSuccess = true;
break;
}
foreach (Node neighbour in NodeGrid.GetNeighbours(currentNode))
{
if (!neighbour.Walkable || closedSet.Contains(neighbour))
{
continue;
}
int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.MovementPenalty;
if (newMovementCostToNeighbor < neighbour.gCost || !openSet.Contains(neighbour))
{
neighbour.gCost = newMovementCostToNeighbor;
neighbour.hCost = GetDistance(neighbour, targetNode);
neighbour.Parent = currentNode;
}
if (!openSet.Contains(neighbour))
{
openSet.Push(neighbour);
}
else
{
openSet.UpdateItem(neighbour);
}
}
}
}
yield return null;
if (pathSuccess)
{
waypoints = RetracePath(startNode, targetNode);
if(waypoints == null || waypoints.Length == 0)
{
waypoints = new Vector3[1];
waypoints[0] = targetNode.WorldPosition;
}
}
RequestManager.FinishedProcessingPath(waypoints, pathSuccess);
}