0

Prompt

You have maps of parts of the space station, each starting at a prison 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 prison 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 answer(map) that generates the length of the shortest path from the prison 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.

Test Cases

maze = [
    [0, 0, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 1, 0],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 0, 0, 0, 0, 1, 0, 1],
    [1, 0, 1, 1, 1, 1, 0, 0],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0]
]
#15

maze = [
    [0, 0, 1, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 1, 0],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 1, 1, 1, 0, 1, 1, 1],
    [1, 0, 0, 0, 0, 1, 0, 1],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0]
]
#21

maze = [
    [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
#39

maze = [
    [0, 1, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 1, 0]
]
#10

My Code

from collections import deque


def solution(map):
    H = len(map)
    W = len(map[0])
    start = ((0, 0), False)

    track = {
        (0, 0): (-1, -1)
    }
    to_explore = deque([start])
    visited = set()
    w, h = 0, 0
    while not (w == W-1 and h == H-1):
        current = to_explore.popleft()
        pos, opened = current
        neighbours = get_neighbours(pos)
        zeroes, ones = check_neighbours(neighbours, map, (W, H))
        for node in zeroes:
            if not node in track and node not in visited:
                form_vertex = (node, opened)
                to_explore.append(form_vertex)
                track[node] = pos
        for node in ones:
            if not node in track and node not in visited and not opened:
                form_vertex = (node, True)
                to_explore.append(form_vertex)
                track[node] = pos
        w, h = pos
        visited.add(pos)

    path = []
    current = (W - 1, H - 1)
    while current != (-1, -1):
        path.append(current)
        current = track[current]
    return len(path)


def get_neighbours(pos):
    w, h = pos
    return [(w+1, h), (w-1, h), (w, h+1), (w, h-1)]


def check_neighbours(neighbours, grid, edge):
    zeroes = []
    ones = []
    W, H = edge
    for neighbour in neighbours:
        w, h = neighbour
        if 0 <= w < W and 0 <= h < H:
            value = grid[h][w]
            if value == 0:
                zeroes.append(neighbour)
            else:
                ones.append(neighbour)

    return zeroes, ones

Problem

The code works as far as I can tell. It passes all the test cases I have thrown at it. However, it fails two of Google's hidden test cases. Can anyone identify the problem and tell me just what I am missing because I have exhausted my own ideas and I can't seem to find just what is wrong.
!! Even if you can't find a solution, failing test cases are appreciated

  • Could you explain what you mean by "Google's hidden test cases"? – Richard Hunter Oct 04 '20 at 22:47
  • @RichardHunter Yes. So we (the user) knows the inputs of some of the test cases, however, there are some test cases whose inputs are hidden. I think this is to prevent people from hard-coding solutions, but the downside is that I cannot tell exactly what error/bug my code is running into. – Rehoboth Okorie Oct 05 '20 at 17:53

0 Answers0