A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Difficulty:Medium
高考题的感觉,注意下int的范围和特殊情况就好了,就是排列组合。
int pro(int a)
{
int ans = 1;
for(int i = 1;i<=a;i++)
ans = ans * i;
return ans;
}
int uniquePaths(int m, int n) {
unsigned long long int ans = 1;
if(m==0||n==0)
return 0;
m--;n--;
if(m==0||n==0)
return 1;
int temp;
if(m<n)
{
temp = m;
m = n;
n =temp;
}
for(int i = m+1; i<=m+n;i++)
ans = ans * i;
ans = ans/pro(n);
return ans;
}