面试题 08.02
Imagine a robot sitting on the upper left corner of grid with r rows and c columns. The robot can only move in two directions, right and down, but certain cells are "off limits" such that the robot cannot step on them. Design an algorithm to find a path for the robot from the top left to the bottom right.
"off limits" and empty grid are represented by 1 and 0 respectively.
Return a valid path, consisting of row number and column number of grids in the path.
Example 1:
Input: [ [0,0,0], [0,1,0], [0,0,0] ] Output: [[0,0],[0,1],[0,2],[1,2],[2,2]] Note:
r, c <= 100
Solutions
dfs with memoization
dynamic programming with backtracking
Record tansitions(this state comes from which) at the time when updating the dp table.
Then after the dp table is filled, backtracking from the final state(right-bottom) to the initial state(0, 0), the traversing path is a valid answer.
Last updated
Was this helpful?