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?
解析:(动态规划)
状态转换方程:d[i][j]=d[i-1][j]+d[i][j-1]
public class Solution {
public int uniquePaths(int m, int n) {
if(m==0||n==0)
return 0;
if(m==1||n==1)
return 1;
int[][] dp=new int[m+1][n+1];
for(int i=0;i<m+1;i++)
{
for(int j=0;j<n+1;j++)
{
if(i==1&&j!=0)
dp[i][j]=1;
else if(i!=0&&j==1)
dp[i][j]=1;
}
}
for(int i=2;i<=m;i++)
{
for(int j=2;j<=n;j++)
{
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
return dp[m][n];
}
}
浙公网安备 33010602011771号