动态规划-机器人位于m x n网格的左上角只能向下向右移动,有多少路径

21 篇文章 0 订阅
21 篇文章 0 订阅
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);
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走在时光柱上的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值