本文整理汇总了C#中OrderedBag.RemoveFirst方法的典型用法代码示例。如果您正苦于以下问题:C# OrderedBag.RemoveFirst方法的具体用法?C# OrderedBag.RemoveFirst怎么用?C# OrderedBag.RemoveFirst使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OrderedBag
的用法示例。
在下文中一共展示了OrderedBag.RemoveFirst方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Dijkstra
private static IList<int> Dijkstra(int start)
{
var distances = Enumerable.Repeat(int.MaxValue, graph.Count).ToArray();
var queue = new OrderedBag<Node>((node1, node2) =>
node1.Distance.CompareTo(node2.Distance)
);
distances[start] = 0;
queue.Add(new Node(start, 0));
while (queue.Count != 0)
{
var currentNode = queue.RemoveFirst();
foreach (var neighborNode in graph[currentNode.To])
{
int currentDistance = distances[currentNode.To] + neighborNode.Distance;
if (currentDistance < distances[neighborNode.To])
{
distances[neighborNode.To] = currentDistance;
queue.Add(new Node(neighborNode.To, currentDistance));
}
}
// Removing repeating is actually slower?
}
return distances;
}
示例2: Calc
static long Calc(long n)
{
if (n == 1)
return 0;
var queue = new OrderedBag<State>((pair1, pair2) =>
pair1.Value.CompareTo(pair2.Value)
);
queue.Add(new State(n, 0));
while (queue.Count != 0)
{
var currentState = queue.RemoveFirst();
if (currentState.Key == 1)
return currentState.Value;
for (int power = 2; power < 60; power++)
{
var powerBase = Math.Pow(currentState.Key, 1.0 / power);
var nextNumber = (long)Math.Round(powerBase);
var nextSteps = Math.Abs(Pow(nextNumber, power) - currentState.Key);
var nextState = new State(nextNumber, currentState.Value + nextSteps + 1);
queue.Add(nextState);
}
}
throw new ArgumentException();
}
示例3: DijkstraFindMinCableLengthBetweenHouses
static void DijkstraFindMinCableLengthBetweenHouses(Dictionary<HouseNode,
List<CableConnection>> graph, HouseNode houseToStartFrom)
{
OrderedBag<HouseNode> houseQueue = new OrderedBag<HouseNode>();
foreach (var house in graph)
{
house.Key.MinCableLenth = int.MaxValue;
}
houseToStartFrom.MinCableLenth = 0;
houseQueue.Add(houseToStartFrom);
while (houseQueue.Count > 0)
{
var currentHouse = houseQueue.RemoveFirst();
if (currentHouse.MinCableLenth == int.MaxValue)
{
break;
}
foreach (var connection in graph[currentHouse])
{
var currentCableLength = currentHouse.MinCableLenth + connection.CableLenth;
if (connection.House.MinCableLenth > currentCableLength)
{
connection.House.MinCableLenth = currentCableLength;
houseQueue.Add(connection.House);
}
}
}
}
示例4: DijkstraAlgorithm
static void DijkstraAlgorithm(Node[] graph, Node source)
{
var priority = new OrderedBag<Node>();
for (int i = 1; i < graph.Length; i++)
{
graph[i].DijkstraDistance = int.MaxValue;
}
source.DijkstraDistance = 0;
priority.Add(source);
while (priority.Count != 0)
{
Node currentNode = priority.RemoveFirst();
if (currentNode.DijkstraDistance == int.MaxValue)
{
break;
}
for (int i = 0; i < currentNode.Edges.Count; i++)
{
int potDistance = currentNode.DijkstraDistance + currentNode.Edges[i].Weight;
if (potDistance < graph[currentNode.Edges[i].NodeId].DijkstraDistance)
{
graph[currentNode.Edges[i].NodeId].DijkstraDistance = potDistance;
priority.Add(graph[currentNode.Edges[i].NodeId]);
}
}
}
}
示例5: Main
public static void Main(string[] args)
{
for (int i = 0; i < 4; i++)
{
var start = new KeyValuePair<BigInteger, int>(1, 0);
BigInteger desired = BigInteger.Parse(Console.ReadLine());
var results = new OrderedBag<KeyValuePair<BigInteger, int>>((x, y) => x.Value.CompareTo(y.Value));
results.Add(start);
var current = start;
HashSet<BigInteger> used = new HashSet<BigInteger>();
while (current.Key != desired)
{
current = results.RemoveFirst();
if (!used.Contains(current.Key))
{
results.Add(new KeyValuePair<BigInteger, int>(current.Key + 1, current.Value + 1));
if (current.Key != 1)
{
BigInteger toPower = 1;
List<BigInteger> powers = new List<BigInteger>();
while (toPower <= desired - 1)
{
toPower *= current.Key;
powers.Add(toPower);
}
powers.Reverse();
foreach (var item in powers)
{
if (item <= desired + 1 && !used.Contains(item))
{
if (!used.Contains(item))
{
results.Add(new KeyValuePair<BigInteger, int>(item, current.Value + 1));
}
if (!used.Contains(item - 1))
{
results.Add(new KeyValuePair<BigInteger, int>(item - 1, current.Value + 2));
}
}
}
}
used.Add(current.Key);
}
}
Console.WriteLine(current.Value);
}
}
示例6: Run
static public Questionnaire Run()
{
Questionnaire questionnaire = new Questionnaire();
OrderedBag<Grille> OPEN = new OrderedBag<Grille>();
OrderedBag<Grille> CLOSE = new OrderedBag<Grille>();
Grille S = new Grille();
OPEN.Add(S);
while (OPEN.Count != 0)
{
Grille n = OPEN.RemoveFirst();
CLOSE.Add(n);
questionnaire.solutionsExplorer.Add(n.getStringEtat());
if (n.getDistanceSolution() == 0)
{
questionnaire.solutionMot = n.getSolutionMot();
questionnaire.solutionVisuelle = n.getSolutionVisuelle();
for (int i = 0; i < questionnaire.solutionVisuelle.Count; i++)
{
Console.Write("\n---Étape" + i + "----\n" + questionnaire.solutionVisuelle[i] + "----------");
}
return questionnaire;
}
foreach (Grille nPrime in n.getListSuccessor())
{
questionnaire.etatExplorer.Add(nPrime.getStringEtat());
if (Contient(OPEN, nPrime) != -1)
{
int position = Contient(OPEN, nPrime);
if (nPrime.getTotalDistance() < OPEN[position].getTotalDistance())
{
OPEN.Remove(OPEN[position]);
OPEN.Add(nPrime);
}
}
else if (Contient(CLOSE, nPrime) != -1)
{
int position = Contient(CLOSE, nPrime);
if (nPrime.getTotalDistance() < CLOSE[position].getTotalDistance())
{
CLOSE.Remove(CLOSE[position]);
OPEN.Add(nPrime);
}
}
else // Ni dans Close , ni dans OPEN
{
OPEN.Add(nPrime);
}
}
}
questionnaire.solutionMot = "Aucun chemin possible";
return questionnaire;
}
示例7: Main
// Almost the same as Programming/5.DataStructuresAndAlgorithms/AlgoAcademy/4.DataStructures/2.Zadachi/Program.cs
static void Main()
{
#if DEBUG
Console.SetIn(new System.IO.StreamReader("../../input.txt"));
#endif
var date = DateTime.Now;
var tasks = new OrderedBag<Tuple<string, int>>((task1, task2) =>
{
int compared = 0;
compared = task1.Item2.CompareTo(task2.Item2);
if (compared != 0) return compared;
compared = task1.Item1.CompareTo(task2.Item1);
if (compared != 0) return compared;
return compared;
});
var output = new StringBuilder();
var separator = new char[] { ' ' };
foreach (int i in Enumerable.Range(0, int.Parse(Console.ReadLine())))
{
string line = Console.ReadLine();
if (line == "Solve")
{
if (tasks.Count == 0)
output.AppendLine("Rest");
else
output.AppendLine(tasks.RemoveFirst().Item1);
}
else
{
var match = line.Split(separator, 3);
tasks.Add(new Tuple<string, int>(match[2], int.Parse(match[1])));
}
}
Console.Write(output);
#if DEBUG
Console.WriteLine(DateTime.Now - date);
#endif
}
示例8: GetMinimalConnectionCost
public static long GetMinimalConnectionCost(string[] input)
{
var graph = new Dictionary<int, List<Edge>>();
foreach (var edge in input)
{
var parts = edge.Split();
var from = int.Parse(parts[0]);
var to = int.Parse(parts[1]);
var cost = int.Parse(parts[2]);
InitializeIfNeeded(graph, from);
InitializeIfNeeded(graph, to);
graph[from].Add(new Edge(from, to, cost));
graph[to].Add(new Edge(to, from, cost));
}
var startNode = graph.First().Key;
var priorityQueue = new OrderedBag<Edge>();
foreach (var neighbour in graph[startNode])
{
priorityQueue.Add(neighbour);
}
var cables = new List<Edge>();
var visitedNodes = new HashSet<int> { startNode };
while (priorityQueue.Count > 0)
{
var min = priorityQueue.RemoveFirst();
if (visitedNodes.Contains(min.To))
{
continue;
}
cables.Add(min);
visitedNodes.Add(min.To);
foreach (var neighbour in graph[min.To])
{
priorityQueue.Add(neighbour);
}
}
return cables.Sum(c => c.Cost);
}
示例9: FindPath
public IEnumerable<Point> FindPath(Point start, Point goal)
{
var startNode = Map[start.Row][start.Column];
var targetNode = Map[goal.Row][goal.Column];
var openNodes = new OrderedBag<Node>(PathComparison);
var closedNodes = new HashSet<Node>();
openNodes.Add(startNode);
while (openNodes.Count > 0)
{
var currentNode = openNodes.RemoveFirst();
closedNodes.Add(currentNode);
if (currentNode == targetNode)
{
return tracePath(targetNode);
}
var neighbours = getNeighbours(currentNode);
foreach (var neighbour in neighbours)
{
if (!neighbour.IsWalkable || closedNodes.Contains(neighbour))
{
continue;
}
var newCost = currentNode.GainedCost + getCost(currentNode, neighbour);
if (newCost < neighbour.GainedCost || !openNodes.Contains(neighbour))
{
neighbour.GainedCost = newCost;
neighbour.HeuristicCost = getDistance(neighbour, targetNode);
neighbour.Parent = currentNode;
if (!openNodes.Contains(neighbour))
{
openNodes.Add(neighbour);
}
}
}
}
//a path doesn't exist
return new List<Point>();
}
示例10: Main
public static void Main()
{
#if DEBUG
Console.SetIn(new System.IO.StreamReader("../../input2.txt"));
#endif
OrderedBag<Task> tasks = new OrderedBag<Task>((a, b) => {
int compared = a.Item1.CompareTo(b.Item1);
if (compared != 0)
{
return compared;
}
return a.Item2.CompareTo(b.Item2);
});
StringBuilder result = new StringBuilder();
int commandsList = int.Parse(Console.ReadLine());
for (int i = 0; i < commandsList; i++)
{
string line = Console.ReadLine();
string[] commandParts = line.Split(new char[]{' '}, 3);
if (commandParts[0] == "New")
{
int priority = int.Parse(commandParts[1]);
string taskName = commandParts[2];
tasks.Add(new Task(priority, taskName));
}
else if (commandParts[0] == "Solve")
{
if (tasks.Count == 0)
{
result.AppendLine("Rest");
continue;
}
Task min = tasks.RemoveFirst();
result.AppendLine(min.Item2);
}
}
Console.Write(result.ToString());
}
示例11: GetDistanceToHouses
private static int GetDistanceToHouses(Node hospital)
{
foreach (var node in map)
{
node.Key.DijkstraDistance = int.MaxValue;
}
hospital.DijkstraDistance = 0;
var nodes = new OrderedBag<Node>();
nodes.Add(hospital);
while (nodes.Count > 0)
{
var currentNode = nodes.RemoveFirst();
if (currentNode.DijkstraDistance == int.MaxValue)
{
break;
}
foreach (var edge in map[currentNode])
{
int potentialDistance = currentNode.DijkstraDistance + edge.Distance;
if (potentialDistance < edge.Destination.DijkstraDistance)
{
edge.Destination.DijkstraDistance = potentialDistance;
nodes.Add(edge.Destination);
}
}
}
int distanceToHomes = 0;
foreach (var point in map)
{
if (!point.Key.IsHospital && point.Key.DijkstraDistance != int.MaxValue)
{
distanceToHomes += point.Key.DijkstraDistance;
}
}
return distanceToHomes;
}
示例12: GetDistanceToHomes
private static int GetDistanceToHomes(Point hospital)
{
foreach (var point in map)
{
point.Key.DijkstraDistance = int.MaxValue;
}
hospital.DijkstraDistance = 0;
OrderedBag<Point> points = new OrderedBag<Point>();
points.Add(hospital);
while (points.Count > 0)
{
Point currentPoint = points.RemoveFirst();
if (currentPoint.DijkstraDistance == int.MaxValue)
{
break;
}
foreach (Connection connection in map[currentPoint])
{
int potentialDistance = currentPoint.DijkstraDistance + connection.Distance;
if (potentialDistance < connection.Destination.DijkstraDistance)
{
connection.Destination.DijkstraDistance = potentialDistance;
points.Add(connection.Destination);
}
}
}
int distanceToHomes = 0;
foreach (var point in map)
{
if (!point.Key.IsHospital && point.Key.DijkstraDistance != int.MaxValue)
{
distanceToHomes += point.Key.DijkstraDistance;
}
}
return distanceToHomes;
}
示例13: GetMinimalspanningTreeUsingPrim
public static List<Edge<int>> GetMinimalspanningTreeUsingPrim(List<Edge<int>> graph, int start)
{
var result = new List<Edge<int>>();
var priority = new OrderedBag<Edge<int>>();
var visited = new HashSet<int>();
visited.Add(start);
foreach (var edge in graph)
{
if (edge.Start == start || edge.End == start)
{
priority.Add(edge);
}
}
while (priority.Count > 0)
{
var current = priority.RemoveFirst();
if (!(visited.Contains(current.Start) && visited.Contains(current.End)))
{
result.Add(current);
}
if (visited.Contains(current.Start) && !visited.Contains(current.End))
{
priority.AddMany(graph.Where(x => x.Start == current.End || x.End == current.End));
visited.Add(current.End);
}
else if (!visited.Contains(current.Start) && visited.Contains(current.End))
{
priority.AddMany(graph.Where(x => x.Start == current.Start || x.End == current.Start));
visited.Add(current.Start);
}
}
return result;
}
示例14: ComputeNextActions
private void ComputeNextActions()
{
float goalDestinationX = player.Location.X + 983;
HashSet<int> visited = new HashSet<int>();
orderedSet = new OrderedBag<PlayerNode>((a, b) => a.Destination((int)goalDestinationX).CompareTo(b.Destination((int)goalDestinationX)));
PlayerNode initialNode = new PlayerNode(null, sceneTime, 0, PlayerAction.None, player.Clone(), 0);
orderedSet.Add(initialNode);
visited.Add((int)initialNode.PlayerElement.Location.X << 10 + (int)initialNode.PlayerElement.Location.Y);
PlayerNode bestNode = initialNode;
bool foundBest = false;
while (orderedSet.Count > 0 && !foundBest)
{
PlayerNode current = orderedSet.RemoveFirst();
if (current.PlayerElement.IsOnGround() && bestNode.Destination((int)goalDestinationX) > current.Destination((int)goalDestinationX))
{
bestNode = current;
}
foreach (PlayerAction action in possibleActions)
{
if (action == PlayerAction.Jump && !current.PlayerElement.IsOnGround())
{
continue;
}
PlayerElement newPlayer = current.PlayerElement.Clone();
newPlayer.IsPlayerAGoat = true;
switch (action)
{
case PlayerAction.None:
newPlayer.Stop();
break;
case PlayerAction.MoveRight:
newPlayer.MoveForward();
break;
case PlayerAction.MoveLeft:
newPlayer.MoveBackward();
break;
case PlayerAction.Jump:
newPlayer.Jump();
break;
}
GameTime newTime = IncrementTime(current.Time);
newPlayer.Update(newTime);
Parallel.ForEach(timeDependentElements, element => element.Update(newTime));
HandleInteraction(newPlayer);
if (newPlayer.IsDead)
{
continue;
}
int hash = ((int)(newPlayer.Location.X * 10) << 7) + (int)newPlayer.Location.Y;
if (!visited.Add(hash))
{
continue;
}
PlayerNode newNode = new PlayerNode(current, newTime, current.Moves + 1, action, newPlayer, current.Jumps + (action == PlayerAction.Jump ? 1 : 0));
if (newPlayer.Location.X > goalDestinationX && newPlayer.IsOnGround() || newPlayer.HasWon)
{
bestNode = newNode;
foundBest = true;
break;
}
orderedSet.Add(newNode);
}
}
orderedSet.Clear();
if (bestNode == initialNode)
{
pendingActions.Enqueue(PlayerAction.None);
}
else
{
GetActionsFromNode(bestNode);
}
}
示例15: Calculator
public RasterOp Calculator(Postion startPostion)
{
if (!Read(startPostion.XIndex,startPostion.YIndex).HasValue)
throw new ArgumentOutOfRangeException("起始位置不含有数据");
RasterPositionValue[,] cost=new RasterPositionValue[Width,Height];
//初始化操作
InitializeValues(cost);
InitializeStart(startPostion,cost);
//使用orderbag,是的每次取出的最小值
OrderedBag<RasterPositionValue> bag=new OrderedBag<RasterPositionValue>();
bag.Add(cost[startPostion.XIndex,startPostion.YIndex]);
while (bag.Count!=0)
{
RasterPositionValue pos = bag.RemoveFirst();
var postions = Sourround(cost, pos);
foreach (var postion in postions)
{
double relativeCost = Read(postion.XIndex, postion.YIndex).GetValueOrDefault() * 0.5 +
Read(postion.XIndex, postion.YIndex).GetValueOrDefault()*0.5;
if (pos.XIndex!=postion.XIndex&&pos.YIndex!=postion.YIndex)
{
relativeCost *= Math.Sqrt(2);
}
cost[postion.XIndex, postion.YIndex].Visited = true;
cost[postion.XIndex, postion.YIndex].HasValue = true;
cost[postion.XIndex, postion.YIndex].RasterValue = (float) relativeCost
+cost[pos.XIndex,pos.YIndex].RasterValue;
bag.Add(cost[postion.XIndex, postion.YIndex]);
}
}
return Result(cost);
}