9.2 Imagine A robot sitting on the upper left corner of the A-X by Y grid. The robot can only have move in, directions:right and down. How many possible paths is there for the robot to go from (0,0) to (x, y)?
Follow up
Imagine certain spots is "off limits," such the robot cannot step on them. Design an algorithm to find a path for the robot from the top of the bottom right.
Leetcode on the original topic, see my previous blog unique Paths different paths and unique Paths II different paths to the second.
Solution One:
classSolution { Public: intGetPath (intXinty) {vector<int> dp (y +1,1); for(inti =1; I <= x; ++i) { for(intj =1; J <= y; ++j) {Dp[j]+ = Dp[j-1]; } } returnDp[y]; }};
Solution Two:
classSolution { Public: intGetPath (intXinty) {Doublenum =1, Denom =1; intsmall = x < y?x:y; for(inti =1; I <= small; ++i) {num*= x + y-i +1; Denom*=i; } return(int) (Num/denom); }};
[Careercup] 9.2 Robot Moving robot mobile