public List<Step[]> GetSolution(BoardState state, BoardGoal goal, CancellationToken cancellationToken) { if (state == null) { throw new ArgumentNullException(nameof(state)); } if (goal == null) { throw new ArgumentNullException(nameof(goal)); } if (state.Width != goal.Width || state.Height != goal.Height) { throw new ArgumentException($"The state ({state.Width}x{state.Height}) and the goal ({goal.Width}x{goal.Height}) has different sizes."); } List<Step[]> solutions = new List<Step[]>(); int targetCost = int.MaxValue; PriorityQueueV5 openSet = new PriorityQueueV5(); openSet.Enqueue(NodeV5.CreateInitialNode(state, goal)); while (true) { NodeV5 current = openSet.Dequeue(); if (current.Cost > targetCost) { break; } if (current.EstimatedDistanceToGoal == 0) { Step[] solution = GetPathFrom(current).Reverse().ToArray(); solutions.Add(solution); if (solution.Length < targetCost) { targetCost = solution.Length; } continue; } foreach (NodeV5 neighbor in current.GetNeighbors(goal)) { if (!openSet.Contains(neighbor)) { openSet.Enqueue(neighbor); } } cancellationToken.ThrowIfCancellationRequested(); } return solutions; }
public List<Step[]> GetSolution(BoardState state, BoardGoal goal, CancellationToken cancellationToken) { if (state == null) { throw new ArgumentNullException(nameof(state)); } if (goal == null) { throw new ArgumentNullException(nameof(goal)); } if (state.Width != goal.Width || state.Height != goal.Height) { throw new ArgumentException($"The state ({state.Width}x{state.Height}) and the goal ({goal.Width}x{goal.Height}) has different sizes."); } PriorityQueueV5 openSet = new PriorityQueueV5(); openSet.Enqueue(NodeV5.CreateInitialNode(state, goal)); while (true) { NodeV5 current = openSet.Dequeue(); if (current.EstimatedDistanceToGoal == 0) { return new List<Step[]> { GetPathFrom(current).Reverse().ToArray() }; } foreach (NodeV5 neighbor in current.GetNeighbors(goal)) { if (!openSet.Contains(neighbor)) { openSet.Enqueue(neighbor); } } cancellationToken.ThrowIfCancellationRequested(); } }