Just to wake up;
980. Unique Paths III
https://leetcode.com/problems/unique-paths-iii/
class Solution:
def checkIfReachable(self,curPoint,walkable, grid):
(i,j) = curPoint
#print(curPoint)
if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]):
return 0
if grid[i][j]==2:
if len(walkable)==0:
return 1
else:
return 0
if curPoint not in walkable:
return 0
walkable.remove(curPoint)
num1 = self.checkIfReachable((i+1,j ),walkable.copy(),grid)
num2 = self.checkIfReachable((i-1,j ),walkable.copy(),grid)
num3 = self.checkIfReachable((i ,j+1),walkable.copy(),grid)
num4 = self.checkIfReachable((i ,j-1),walkable.copy(),grid)
return num1+num2+num3+num4
def uniquePathsIII(self, grid: List[List[int]]) -> int:
walkable = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 0:
walkable.append((i,j))
elif grid[i][j] == 1:
walkable.append((i,j))
start = (i,j)
numPath = self.checkIfReachable(start,walkable,grid)
return numPath
0 件のコメント:
コメントを投稿