Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Prepare the Bunnies' Escape
- # ===========================
- # You're awfully close to destroying the LAMBCHOP doomsday device and freeing Commander Lambda's bunny workers,
- # but once they're free of the work duties the bunnies are going to need to escape Lambda's space station
- # via the escape pods as quickly as possible.
- # Unfortunately, the halls of the space station are a maze of corridors
- # and dead ends that will be a deathtrap for the escaping bunnies.
- # Fortunately, Commander Lambda has put you in charge of a remodeling project
- # that will give you the opportunity to make things a little easier for the bunnies.
- # Unfortunately (again), you can't just remove all obstacles between the bunnies and the escape pods -
- # at most you can remove one wall per escape pod path,
- # both to maintain structural integrity of the station and to avoid arousing Commander Lambda's suspicions.
- #
- # You have maps of parts of the space station, each starting at a work area exit and ending at the door to an escape pod.
- # The map is represented as a matrix of 0s and 1s, where 0s are passable space and 1s are impassable walls.
- # The door out of the station is at the top left (0,0) and the door into an escape pod is at the bottom right (w-1,h-1).
- #
- # Write a function solution(map) that generates the length of the shortest path from the station door to the escape pod,
- # where you are allowed to remove one wall as part of your remodeling plans.
- # The path length is the total number of nodes you pass through, counting both the entrance and exit nodes.
- # The starting and ending positions are always passable (0).
- # The map will always be solvable, though you may or may not need to remove a wall.
- # The height and width of the map can be from 2 to 20.
- # Moves can only be made in cardinal directions; no diagonal moves are allowed.
- from queue import Queue
- def get_neighbors(x: int, y: int, max_x: int, max_y: int) -> list:
- return list(filter(None, [
- [x-1, y] if x > 1 else None,
- [x, y+1] if y < max_y else None,
- [x+1, y] if x < max_x else None,
- [x, y-1] if y > 1 else None
- ]))
- def solution(layout: list[list[int]]) -> int:
- max_x = len(layout[0])
- max_y = len(layout)
- frontier = Queue()
- frontier.put([0, 0])
- reached = set()
- reached.add((0, 0))
- while not frontier.empty():
- current_x, current_y = frontier.get()
- neighbors = get_neighbors(current_x, current_y, max_x, max_y)
- for x, y in neighbors:
- if layout[x][y] == 1:
- continue
- for new_x, new_y in get_neighbors(x, y, max_x, max_y):
- if (new_x, new_y) not in reached and layout[new_x][new_y] == 0:
- frontier.put(new_x, new_y)
- reached.add((x, y))
- return int()
- assert solution([
- [0, 0, 0, 0, 0, 0],
- [1, 1, 1, 1, 1, 0],
- [0, 0, 0, 0, 0, 0],
- [0, 1, 1, 1, 1, 1],
- [0, 1, 1, 1, 1, 1],
- [0, 0, 0, 0, 0, 0]]) == 11
- assert solution([
- [0, 1, 1, 0],
- [0, 0, 0, 1],
- [1, 1, 0, 0],
- [1, 1, 1, 0]]) == 7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement