コード例 #1
0
class Solver():
    def __init__(self, board):
        self.board = board
        self.twin_board = self.board.twin()

        self.data_structure = DataStructure()
        self.twin_data_structure = DataStructure()

        self.states_explored = -1

        current_state = self.board
        current_twin_state = self.twin_board
        while True:
            self.states_explored += 1
            # print(current_state.moves_to_reach)
            # print(current_state)
            if current_state.is_solved():
                self.solution = self._form_solution(current_state)
                break
            elif current_twin_state.is_solved():
                self.moves = -1
                self.solution = []
                break

            for neighbor in current_state.neighbors():
                if current_state.previous is None or not neighbor.has_same_data(current_state.previous):
                    self.data_structure.add(neighbor)

            for neighbor in current_twin_state.neighbors():
                if current_twin_state.previous is None or not neighbor.has_same_data(current_twin_state.previous):
                    self.twin_data_structure.add(neighbor)

            current_state = self.data_structure.get_min()
            current_twin_state = self.twin_data_structure.get_min()

    def _form_solution(self, last_state):
        solution = []
        state = last_state
        while state.previous is not None:
            solution.append(state)
            state = state.previous
        solution.append(state)
        solution.reverse()
        return solution