def manhattan_dist_sum(p, tiles = None): if not tiles: tiles = p.flat solved = puzzle.solved(p) s = 0 for t in tiles: s_pos = puzzle.get_position(p, t) t_pos = puzzle.get_position(solved, t) s += u.manhattan_distance(*s_pos, *t_pos) return s
def rec_a_star(p, selector = SubSelect()): solved = puzzle.solved(p) path = [] while selector.applicable(p): tiles_to_solve = [t for t in solved.flat if t not in selector.apply(solved)] heuristic = lambda puz: manhattan_dist_sum(puz, tiles_to_solve) new_path = ida_star(p, heuristic) path += new_path p = selector.apply(puzzle.apply_actions(p, new_path)) solved = selector.apply(solved) path += a_star(p, manhattan_dist_sum) return path
def __init__(self, p, moves, transp = False): self.locked = set() self.transposed = transp self.p = p.transpose() if transp else p self.s = puz.solved(p).transpose() if transp else puz.solved(p) self._moves = [transpose_action(m) for m in moves] if transp else moves