public void idfs(PuzzleGrid grid, int depth) { if (depth < 0) { return; // end of recursion } //check if reached solution, to stop the algorithm if (grid.checkIfSolved()) { Console.WriteLine("SOLVED!"); grid.printGrid(); this.goal = grid; return; } PuzzleGrid newPuzzleState; foreach (char i in possible_moves) { //Console.WriteLine(i); //Console.WriteLine(grid.move(i)); newPuzzleState = grid.move(i); if (newPuzzleState != null && !doneMoves.Contains(newPuzzleState)) { CallIDFS(newPuzzleState, depth); // newPuzzleState.printGrid(); } } }
public bool SMA(PuzzleGrid grid, int heuristic_id) { SimplePriorityQueue <PuzzleGrid> frontier = new SimplePriorityQueue <PuzzleGrid>(); HashSet <PuzzleGrid> doneMoves = new HashSet <PuzzleGrid>(); frontier.Enqueue(grid, 0); doneMoves.Add(grid); var previousCost = 0; while (frontier.Count != 0) { grid = frontier.Dequeue(); foreach (char i in possible_moves) { var newPuzzleState = grid.move(i); if (newPuzzleState == null) { continue; } if (newPuzzleState.checkIfSolved()) { Console.WriteLine("SOLVED!"); newPuzzleState.printGrid(); return(true); } if (!doneMoves.Contains(newPuzzleState)) { grid._level_of_depth++; int priorityLevel = 0; if (heuristic_id == 1) { priorityLevel = newPuzzleState.manhatann_heuristic() + grid._level_of_depth; } else if (heuristic_id == 2) { priorityLevel = newPuzzleState.diagonal_heuristic() + grid._level_of_depth; } else if (heuristic_id == 3) { priorityLevel = (int)newPuzzleState.euclides_heuristic() + grid._level_of_depth; } frontier.Enqueue(newPuzzleState, Math.Max(priorityLevel, previousCost)); previousCost = priorityLevel; doneMoves.Add(newPuzzleState); } } } return(false); }
public Boolean DFS(PuzzleGrid grid) { Stack <PuzzleGrid> frontier = new Stack <PuzzleGrid>(); HashSet <PuzzleGrid> doneMoves = new HashSet <PuzzleGrid>(); frontier.Push(grid); doneMoves.Add(grid); while (frontier.Count != 0) { grid = frontier.Pop(); foreach (char i in possible_moves) { var newPuzzleState = grid.move(i); if (newPuzzleState == null) { continue; } if (newPuzzleState.checkIfSolved()) { Console.WriteLine("SOLVED!"); newPuzzleState.printGrid(); return(true); } if (!doneMoves.Contains(newPuzzleState)) { frontier.Push(newPuzzleState); doneMoves.Add(newPuzzleState); } } } return(false); }