Пример #1
0
def compute_all_moves(start_state, end_state):
    """
    Computes all possible ways to rearrange the cars from
    start_state to end_state.

    :param start_state: order of cars in the start of the rearrangement
    :param end_state: order of cars after rearrangement
    :yields: a list of move steps. Each move is represented as a tuple
             with two indeces,
             the 1st index is the number of lot from which we move the car,
             the 2nd index is the number of lot to which we move the car
    """
    check_input_validity(start_state, end_state)
    parking_size = len(start_state)
    constraints = {i: tuple(range(parking_size)) for i in range(parking_size)}
    path_finder = PathFinder(tuple(start_state),
                             tuple(end_state),
                             constraints=constraints)
    paths = path_finder.find_all_paths()
    for path in paths:
        decoded_path = path_finder.decode_path(path)
        moves_sequence = list()
        for i in range(1, len(decoded_path)):
            moves_sequence.append(
                compute_move(decoded_path[i - 1], decoded_path[i]))
        yield moves_sequence
Пример #2
0
def compute_moves_with_constraints(start_state, end_state, constraints):
    """
    Computes moves sequence under given constraints.
    :param start_state: order of cars in the start of the rearrangement
    :param end_state: order of cars after rearrangement
    :param constraints: map from the parking lot to a tuple of the allowed cars
    :yields: move steps. Each move is represented as a tuple with two indices,
             the 1st index is the number of lot from which we move the car,
             the 2nd index is the number of lot to which we move the car
    """
    check_input_validity(start_state, end_state)
    path_finder = PathFinder(tuple(start_state), tuple(end_state), constraints)
    paths = path_finder.find_all_paths()
    path = path_finder.decode_path(next(paths))
    for i in range(1, len(path)):
        yield compute_move(path[i - 1], path[i])