62.Unique Paths (法1递归-动态规划法2数学公式)

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. Therobot 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?

Note: m and n willbe at most 100.

HideTags

Array Dynamic
Programming

#pragma once
#include<iostream>
using namespace std;

//法1:递归,超时
int uniquePaths1(int m, int n)
{
	if (m == 2 || n == 2)
		return m+n-2;
	return uniquePaths1(m - 1, n) + uniquePaths1(m, n - 1);

}

//法2:数学公式 求m+n-2中取m-1的组合数
int uniquePaths2(int m, int n)
{
	long long result = 1;
	int reduce = m - 1;
	for (int i = m - 1; i >= 1; i--)
	{
		result *= (n - 1 + i);
		while (reduce>0 && result%reduce == 0)
		{
			result = result / reduce;
			reduce--;
		}
	}
	//内层循环用while后,由于最后结果一定是能整除的,所以不用再加以下
	/*while (reduce > 0)
	{
		result = result / reduce;
	}*/
	return result;
}

void main()
{
	cout << uniquePaths2(36, 7) << endl;
	cout << uniquePaths1(36, 7) << endl;
	system("pause");
}
时间: 2024-10-26 21:11:20

62.Unique Paths (法1递归-动态规划法2数学公式)的相关文章

刷题62. Unique Paths

一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题目,理解后不难.定义一个dp[m][n],初始化最后一列为1,最后一行为1,然后循环计算到dp[0][0]就可以了. 代码如下: class Solution{ public: int uniquePaths(int m,int n){ vector<vector<int>> dp(m

LeetCode --- 62. Unique Paths

题目链接:Unique Paths 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 (ma

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

62. Unique Paths i &amp; ii

62. Unique Paths 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 (mar

leetCode 62.Unique Paths (唯一路径) 解题思路和方法

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 t

【一天一道LeetCode】#62. Unique Paths

一天一道LeetCode系列 (一)题目 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

19.2.9 [LeetCode 62] Unique Paths

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 t

62. Unique Paths

题目: A robot is located at the top-left corner of a m x ngrid (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' i

62. Unique Paths (Graph; DP)

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 t