dp一下就行- - 其实这是一个组合数学题,答案是C(m - 1 + n - 1, m - 1)
const int MAXN = 100 + 10;
int dp[MAXN][MAXN];
class Solution {
public:
Solution(){
memset(dp, 0xff, sizeof(dp));
for(int i = 1; i < MAXN; ++ i)
dp[1][i] = dp[i][1] = 1;
}
int uniquePaths(int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int &ret = dp[m][n];
if(ret != -1)
return ret;
return ret = uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
}
};