package com.algorithm.dynamicprogramming;
/**
* 算法描述:机器人位于m x n网格的左上角(下图中标记为“开始”)。
* 机器人只能在任何时间点向下或向右移动。机器人正试图到达网格的右下角(下图中标记为“Finish”)。 有多少种可能的唯一路径? 上面是一个7 x
* 3的网格。有多少种可能的唯一路径? 注:m和n最多为100。
*
* Example 1:
* Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there
* are a total of 3 ways to reach the bottom-right corner: 1. Right -> Right ->
* Down 2. Right -> Down -> Right 3. Down -> Right -> Right
*
* Example 2:
* Input: m = 7, n = 3 Output: 28
* @author rich
*
*/
public class DistinctPaths {
public static void main(String[] args) {
System.out.println(distinctPaths(7, 3));
}
/**
* 算法分析:
* 当 m = 1或者 n = 1时只有一行或者一列 只有一种路径
* 否则 f(m,n) = f(m-1,n)+f(m,n-1)
* @param m
* @param n
* @return
*/
public static int distinctPaths(int m,int n) {
if (m == 1 || n == 1) return 1;
return distinctPaths(m-1,n) + distinctPaths(m,n-1);
}
}
动态规划-机器人位于m x n网格的左上角只能向下向右移动,有多少路径
最新推荐文章于 2024-04-01 23:47:02 发布