Robot in a Grid: 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.
Hints: #331, #360, #388
解法1
机器人从(1, 1)
开始:
- 右移,在剩下的
(2, 1)
~(r, c)
网格里找路线 - 下移,在剩下的
(1, 2)
~(r, c)
网格里找路线
然后以此类推。
Base Condition:
- 当
x == r
时只能右移 - 当
c == 1
时只能下移 - 当遇到坏掉的cell的时候停止探索,报告失败
- 当遇到右下角的时候停止探索,报告成功
代码:
|
|
评论