Created
December 20, 2019 13:10
-
-
Save rishi93/4ced0720ddea4d6e4f19c2e7d450f89a to your computer and use it in GitHub Desktop.
Daily Coding Problem - 23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This problem was asked by Google. | |
You are given an M by N matrix consisting of booleans that represents a board. Each True boolean represents a wall. Each False boolean represents a tile you can walk on. | |
Given this matrix, a start coordinate, and an end coordinate, return the minimum number of steps required to reach the end coordinate from the start. If there is no possible path, then return null. You can move up, left, down, and right. You cannot move through walls. You cannot wrap around the edges of the board. | |
For example, given the following board: | |
[[f, f, f, f], | |
[t, t, f, t], | |
[f, f, f, f], | |
[f, f, f, f]] | |
and start = (3, 0) (bottom left) and end = (0, 0) (top left), the minimum number of steps required to reach the end is 7, since we would need to go through (1, 2) because there is a wall everywhere else on the second row. | |
""" | |
import math | |
def solveMaze(matrix, M, N, startX, startY, stopX, stopY): | |
def helper(matrix, costs, M, N, startX, startY, stopX, stopY): | |
# Move up | |
# Check if legal move | |
if startX-1 >= 0 and matrix[startX-1][startY] == 'f': | |
# Check if move improves cost | |
if 1 + costs[startX][startY] < costs[startX-1][startY]: | |
costs[startX-1][startY] = 1 + costs[startX][startY] | |
helper(matrix, costs, M, N, startX-1, startY, stopX, stopY) | |
# Move left | |
# Check if legal move | |
if startY-1 >= 0 and matrix[startX][startY-1] == 'f': | |
# Check if move improves cost | |
if 1 + costs[startX][startY] < costs[startX][startY-1]: | |
costs[startX][startY-1] = 1 + costs[startX][startY] | |
helper(matrix, costs, M, N, startX, startY-1, stopX, stopY) | |
# Move down | |
# Check if legal move | |
if startX+1 < M and matrix[startX+1][startY] == 'f': | |
# Check if move improves cost | |
if 1 + costs[startX][startY] < costs[startX+1][startY]: | |
costs[startX+1][startY] = 1 + costs[startX][startY] | |
helper(matrix, costs, M, N, startX+1, startY, stopX, stopY) | |
# Move right | |
# Check if legal move | |
if startY+1 < N and matrix[startX][startY+1] == 'f': | |
# Check if move improves cost | |
if 1 + costs[startX][startY] < costs[startX][startY+1]: | |
costs[startX][startY+1] = 1 + costs[startX][startY] | |
helper(matrix, costs, M, N, startX, startY+1, stopX, stopY) | |
costs = [[math.inf for j in range(0, N)] for i in range(0, M)] | |
costs[startX][startY] = 0 | |
helper(matrix, costs, M, N, startX, startY, stopX, stopY) | |
return costs[stopX][stopY] | |
matrix =[['f', 'f', 'f', 'f'], | |
['t', 't', 'f', 't'], | |
['f', 'f', 'f', 'f'], | |
['f', 'f', 'f', 'f']] | |
print(solveMaze(matrix, 4, 4, 3, 0, 0, 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment