Problem 3 Unique Path III
https://leetcode.com/problems/unique-paths-iii/description/
思考
注意对比经典模型:二方向,左上道右下,有多少总方法到达:dynamic programming模型
Step 1:
这是个graph的问题
vertex: matrix里的每一个坐标,edge:上下左右四个方向
Step 2:道出本质
人家问我们一共有多少种走出所有空地从start道end
从start到end所有可能的走过所有空地的路径数量
Method Backtracking(dfs mark visited3)
High Level
每一层:走一步
分支:下一步往哪里走,不出界不是障碍物
一共多少层:空地层
Detail Logic
walk over every non-obstacle square exactly once != walk over every empty square exactly once
1 representing the starting square. There is exactly one starting square
2 representing the ending square. There is exactly one ending square
0 representing empty squares we can walk over
-1 representing obstacle that we cannot walk over
没给start and end 怎么办?只能遍历
你怎么知道你走没有走完所有non -obstacles cell呢?
必须要保证世纪走过的空地数 = 总的空地数
TC & SC
TC : O(3^m*n)
SC: O(m* n)
Last updated