题目描述:
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?
Note: m and n will be at most 100.
(一个机器人位于一个M×N的网格的左上角。(在下图中标记为"起点")。
机器人只能向下或向右移动。
机器人正试图到达网格的右下角。(在下图中标明"终点")。
有多少种可能的独特路径?
注:m和n最多为100。)
思路:board[i][j]的路的数量就是到其上面的路的数量和其左面的路的数量的总和。
public class Unique_Paths {
public static int uniquePaths(int m, int n)
{
//DP[i][j]表示到board[i][j]有几条路
int DP[][] = new int[m][n];
DP[0][0] = 1;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i-1>=0)
DP[i][j] += DP[i-1][j];
if(j-1>=0)
DP[i][j] += DP[i][j-1];
}
}
return DP[m-1][n-1];
}
public static void main(String[] args) {
int m = 3;
int n = 4;
System.out.println(uniquePaths(m,n));
}
}
还有一个题 Unique Paths2和这个类似。