Print step-by-step solution from initial to goal state.
(solution: Optional[PuzzleState])
| 85 | |
| 86 | |
| 87 | def print_solution(solution: Optional[PuzzleState]) -> None: |
| 88 | """Print step-by-step solution from initial to goal state.""" |
| 89 | if not solution: |
| 90 | print("No solution found.") |
| 91 | return |
| 92 | steps = [] |
| 93 | while solution: |
| 94 | steps.append(solution.board) |
| 95 | solution = solution.previous |
| 96 | for step in reversed(steps): |
| 97 | for row in step: |
| 98 | print(" ".join(map(str, row))) |
| 99 | print() |
| 100 | |
| 101 | |
| 102 | if __name__ == "__main__": |