2
def shortestPath(self, grid: List[List[int]], k: int) -> int:3
Q = [[0, 0, k]] # m, n, remaining elimination quota4
rows, cols = len(grid), len(grid[0])7
}, 0 # I use a V to keep track of how cells have been visited12
if m == rows - 1 and n == cols - 1:14
for dm, dn in [[1, 0], [-1, 0], [0, 1], [0, -1]]:15
if 0 <= m + dm < rows and 0 <= n + dn < cols: # check inbound16
if grid[m + dm][n + dn] == 0:17
if (m + dm, n + dn) not in V or V[19
] < rem: # if not visited or could be visited with fewer elimination20
frontier.append([m + dm, n + dn, rem])21
V[(m + dm, n + dn)] = rem22
elif rem > 0: # I see a wall and I can still eliminate23
if (m + dm, n + dn) not in V or V[25
] < rem - 1: # if not visited or could be visited with fewer elimination26
frontier.append([m + dm, n + dn, rem - 1])27
V[(m + dm, n + dn)] = rem - 1