Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- my path finding function for kirk:
- def build_path(
- curr_pos,
- tar_pos,
- values,
- shortest_path_discovered=None,
- curr_sides=None,
- curr_path=None,
- first_call=True,
- ):
- curr_path = curr_path or []
- curr_sides = curr_sides or (UP, DOWN)
- shortest_path_discovered = shortest_path_discovered or [UP] * MAX_MOVES
- values = values.copy()
- if curr_pos == tar_pos:
- return curr_path # We're here!
- if values[tar_pos] == '?':
- if dist(curr_pos, tar_pos) == 1: # We got as close as possible to unknown
- return curr_path
- if len(curr_path) > len(shortest_path_discovered):
- return False # A shorter path was found before
- safe_positions = build_line(curr_pos, values, curr_sides)
- # debug_path_finding(dist=dist(curr_pos, tar_pos), **locals())
- for pos in safe_positions:
- values[pos] = 'W'
- next_sides = (UP, DOWN) if curr_sides[0] == RIGHT else (RIGHT, LEFT)
- for pos in safe_positions:
- if pos == curr_pos and not first_call:
- continue
- path = build_path(
- pos,
- tar_pos,
- values,
- shortest_path_discovered,
- next_sides,
- curr_path + [pos],
- first_call=False,
- )
- if path and len(path) < len(shortest_path_discovered):
- shortest_path_discovered = path
- if dist(curr_pos, tar_pos) >= len(shortest_path_discovered):
- return shortest_path_discovered # won't get quicker
- if len(shortest_path_discovered) < MAX_MOVES:
- return shortest_path_discovered # we found a way
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement