Created
June 18, 2020 14:01
-
-
Save ranasaani/125552746936225aa8998a5a84c42dac to your computer and use it in GitHub Desktop.
675. Cut Off Trees for Golf Event
This file contains 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
# 675. Cut Off Trees for Golf Event | |
# Maze with obstacles | |
class Solution: | |
def cutOffTree(self, forest: List[List[int]]) -> int: | |
R, C = len(forest), len(forest[0]) | |
queue = [(0,0,0)] # sr, sc, steps | |
target = (0,0) | |
maxWeight = 0; | |
for r, row in enumerate(forest): | |
for c , column in enumerate(row): | |
if column > maxWeight: | |
maxWeight = column | |
target = (r,c) | |
seen = {(0,0)} | |
while queue: | |
sr, sc, steps = queue.pop(0) | |
if (sr,sc) == target: | |
return steps | |
for r,c in [(sr -1 , sc),(sr + 1, sc), (sr, sc -1), (sr, sc + 1)]: | |
if 0 <= r < R and 0 <= c < C and forest[r][c] and (r,c) not in seen: | |
seen.add((r,c)) | |
queue.append((r, c, steps+1)) | |
return -1 | |
s = Solution() | |
print(s.cutOffTree([[1,2,3],[0,0,4],[7,6,5]])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment