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 Boolean BFS(PuzzleGrid grid) { Queue <PuzzleGrid> frontier = new Queue <PuzzleGrid>(); HashSet <PuzzleGrid> doneMoves = new HashSet <PuzzleGrid>(); frontier.Enqueue(grid); doneMoves.Add(grid); while (frontier.Count != 0) { grid = frontier.Dequeue(); foreach (char i in possible_moves) { var newPuzzleState = grid.move(i); if (newPuzzleState == null) { continue; } if (grid.checkIfSolved()) { Console.WriteLine("SOLVED!"); grid.printGrid(); return(true); } if (!doneMoves.Contains(newPuzzleState)) { frontier.Enqueue(newPuzzleState); doneMoves.Add(newPuzzleState); } } } return(false); }