Generate all valid neighboring states by moving empty tile (0).
(self)
| 45 | return self.board == self.goal |
| 46 | |
| 47 | def neighbors(self) -> List["PuzzleState"]: |
| 48 | """Generate all valid neighboring states by moving empty tile (0).""" |
| 49 | neighbors = [] |
| 50 | x, y = next((i, j) for i in range(3) for j in range(3) if self.board[i][j] == 0) |
| 51 | for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: |
| 52 | nx, ny = x + dx, y + dy |
| 53 | if 0 <= nx < 3 and 0 <= ny < 3: |
| 54 | new_board = [row[:] for row in self.board] |
| 55 | new_board[x][y], new_board[nx][ny] = new_board[nx][ny], new_board[x][y] |
| 56 | neighbors.append( |
| 57 | PuzzleState(new_board, self.goal, self.moves + 1, self) |
| 58 | ) |
| 59 | return neighbors |
| 60 | |
| 61 | |
| 62 | def solve_puzzle( |
no test coverage detected