public static void Simple_Priority_Queue() { var pq = new PrioritySet <string, int>(); pq.Enqueue("John", 1940); pq.Enqueue("Paul", 1942); pq.Enqueue("George", 1943); pq.Enqueue("Ringo", 1941); Assert.Equal("John", pq.Dequeue()); Assert.Equal("Ringo", pq.Dequeue()); Assert.Equal("Paul", pq.Dequeue()); Assert.Equal("George", pq.Dequeue()); }
public void Initialize() { var random = new Random(42); _priorities = new int[Size]; for (int i = 0; i < Size; i++) { _priorities[i] = random.Next(); } _prioritySet = new PrioritySet <int, int>(initialCapacity: Size); _pairingHeap = new PairingHeap <int, int>(Comparer <int> .Default); _pairingHeapNodes = new Dictionary <int, PairingHeapNode <int, int> >(Size); }
public void Initialize() { var random = new Random(42); _priorities = new int[2 * Size]; for (int i = 0; i < 2 * Size; i++) { _priorities[i] = random.Next(); } _priorityQueue2 = new PriorityQueue <int>(initialCapacity: Size); _priorityQueue = new PriorityQueue <int, int>(initialCapacity: Size); _prioritySet = new PrioritySet <int, int>(initialCapacity: Size); _pairingHeap = new PairingHeap <int, int>(Comparer <int> .Default); }
private static int Solve(int bossHp, int bossDamage, bool hasExtraDamage) { const int startingMana = 500; const int startingHp = 50; var pq = new PrioritySet <GameState, int>(); var firstState = new GameState(startingMana, startingHp, bossHp, 0, 0, 0); pq.Enqueue(firstState, 0); while (pq.TryDequeue(out GameState state, out int usedMana)) { // check if boss is dead if (state.BossHP == 0) { return(usedMana); } // If it is part 2, apply damage to the player if (hasExtraDamage) { int newHp = state.PlayerHP - 1; if (newHp == 0) { continue; } state = state with { PlayerHP = newHp }; } // Apply all active effects if (state.Shield > 0) { state = state with { Shield = state.Shield - 1 }; } if (state.Poison > 0) { state = state with { BossHP = Math.Max(0, state.BossHP - 3), Poison = state.Poison - 1 }; if (state.BossHP == 0) { return(usedMana); } } if (state.Recharge > 0) { state = state with { Mana = state.Mana + 101, Recharge = state.Recharge - 1 }; } // Try use magic missile if (state.Mana >= 53) { GameState stateAfterPlayerTurn = state with { BossHP = Math.Max(state.BossHP - 4, 0), Mana = state.Mana - 53 }; if (SimulateBossTurn(stateAfterPlayerTurn, bossDamage, out GameState stateAfterBossTurn)) { pq.EnqueueOrUpdate(stateAfterBossTurn, usedMana + 53); } } // Try use drain if (state.Mana >= 73) { GameState stateAfterPlayerTurn = state with { BossHP = Math.Max(state.BossHP - 2, 0), PlayerHP = state.PlayerHP + 2, Mana = state.Mana - 73 }; if (SimulateBossTurn(stateAfterPlayerTurn, bossDamage, out GameState stateAfterBossTurn)) { pq.EnqueueOrUpdate(stateAfterBossTurn, usedMana + 73); } } // Try use shield if (state.Mana >= 113 & state.Shield == 0) { GameState stateAfterPlayerTurn = state with { Shield = 6, Mana = state.Mana - 113 }; if (SimulateBossTurn(stateAfterPlayerTurn, bossDamage, out GameState stateAfterBossTurn)) { pq.EnqueueOrUpdate(stateAfterBossTurn, usedMana + 113); } } // Try use poison if (state.Mana >= 173 & state.Poison == 0) { GameState stateAfterPlayerTurn = state with { Poison = 6, Mana = state.Mana - 173 }; if (SimulateBossTurn(stateAfterPlayerTurn, bossDamage, out GameState stateAfterBossTurn)) { pq.EnqueueOrUpdate(stateAfterBossTurn, usedMana + 173); } } // Try use recharge if (state.Mana >= 229 & state.Recharge == 0) { GameState stateAfterPlayerTurn = state with { Recharge = 5, Mana = state.Mana - 229 }; if (SimulateBossTurn(stateAfterPlayerTurn, bossDamage, out GameState stateAfterBossTurn)) { pq.EnqueueOrUpdate(stateAfterBossTurn, usedMana + 229); } } } ThrowHelper.ThrowException("Unable to beat the boss"); return(default);