Topic
There is a robot that is located in the upper left corner of an MXN grid (labeled ' Start ' in the middle).
Robots can only move one step down or to the right each time. The robot tries to reach the lower-right corner of the grid (labeled ' Finish '). Q. How many different paths are there?
Precautions
N and M are not more than 100
Sample Example
| The |
The |
1,3 |
1,4 |
1,5 |
1,6 |
1,7 |
| 2,1 |
|
|
|
|
|
|
| 3,1 |
|
|
|
|
|
3,7 |
How many different paths are there in the above 3 x 7 grid?
Ideas
DP's idea to solve
DP[0][J] = 1;
Dp[i][0] = 1;
DP[I][J] = Dp[i-1][j] + dp[i][j-1];
Space complexity can be reduced to O (n)
Dp[i] = Dp[i] + dp[i-1];
C + + code
1 intUniquepaths (intMintN) {2 //Wirte Your code here3vector<int> DP (N,1);4 for(inti =1; I < m; ++i)5 {6 for(intj =1; J < N; ++j)7 {8DP[J] + = dp[j-1];9 }Ten } One returnDp[n-1]; A}View Code
lintcode_114 a different path