r 求唯一路径_唯一路径数

r 求唯一路径

Description:

描述:

In this article, we are going to see a standard dynamic problem which can be featured in any interview coding.

在本文中,我们将看到一个标准的动态问题,该问题可以在任何采访编码中体现。

Problem statement:

问题陈述:

Given a M X N matrix with initial position at top-left cell, find the number of possible unique paths to reach the bottom right cell of the matrix from the initial position. Possible moves can be either down or right at any point in time.

给定一个MXN矩阵 ,其初始位置位于左上角单元格, 找到从初始位置到达矩阵的右下角单元格的可能唯一路径的数量 。 在任何时间点,可能的移动都可以向下向右

    Input:
    The first line contains an integer T, 
    depicting total number of test cases. 
    Then following T lines contains two integers 
    m and n depicting the size of the grid.
    
    Output:
    Print the number of unique paths to reach 
    bottom-right cell from the top-left cell.

Example with explanation:

带有说明的示例:

    Input:
    2
    3 3
    3 4

    Output:
    6
    10

So, for the first test case, M=3, n=3

因此,对于第一个测试用例, M = 3,n = 3

Grid is like,

网格就像

Number of Unique Paths (1)

1. (0,0)->(0,1)->(0,2)->(1,2)->(2,2)

1.(0,0)->(0,1)->(0,2)->(1,2)->(2,2)

Number of Unique Paths (2)

2. (0,0)->(0,1)->(1,1)->(1,2)->(2,2)

2.(0,0)->(0,1)->(1,1)->(1,2)->(2,2)

Number of Unique Paths (3)

3. (0,0)->(0,1)->(1,1)->(2,1)->(2,2)

3.(0,0)->(0,1)->(1,1)->(2,1)->(2,2)

Number of Unique Paths (3)

4. (0,0)->(1,0)->(2,0)->(2,1)->(2,2)

4.(0,0)->(1,0)->(2,0)->(2,1)->(2,2)

Number of Unique Paths (5)

5. (0,0)->(1,0)->(1,1)->(1,2)->(2,2)

5.(0,0)->(1,0)->(1,1)->(1,2)->(2,2)

Number of Unique Paths (6)

6. (0,0)->(1,0)->(1,1)->(2,1)->(2,2)

6.(0,0)->(1,0)->(1,1)->(2,1)->(2,2)

Number of Unique Paths (7)

This are the possible unique paths and hence count is 6.

这是可能的唯一路径,因此计数为6。

Given a matrix with an initial position at the top-left cell, find the number of possible unique paths to reach the bottom-right cell of the matrix from the initial position. Possible moves can be either or at any point in time.

给定一个矩阵,该矩阵在左上角的单元格处具有初始位置,请找到从初始位置到达矩阵的右下角的单元格的可能唯一路径的数量。 可能的移动可以是任意时间,也可以在任何时间点。

Recursion Algorithm:

递归算法:

    Of course, we can generate a recursion algorithm.
    Since the only possible moves are down and right 
    from any point, we can write a recursive relation,

    f(m,n) = f(m-1,n) + f(m,n-1)
    Where f(m,n) = number of unique path for grid size m,n
    f(m-1,n) = number of unique path for grid size [(m-1),n] 
    a down move from the end point of this will result in f(m,n)
    f(m,n-1) = number of unique path for grid size [m,(n-1)] 
    a right move from the end point of this will result in f(m,n)
    Hence, f(m,n) is summation of f(m-1,n) and f(m,n-1)

So, the algorithm can be:

因此,该算法可以是:

    Function Uniquepaths(m,n):
        1)  If m==0 & n==0 return 0
        2)  If m == 0
                Then return 1
        3)  If n==0
                Then return 1
        4) Return Uniquepaths(m-1,n)+Uniquepaths(m,n-1)

But this would generate many overlapping subproblems, which will be computed again and again increasing the time complexity. Hence, we can convert the recursion to dynamic Programming.

但这将产生许多重叠的子问题,这些子问题将被反复计算,从而增加了时间复杂度。 因此,我们可以将递归转换为动态编程。

Dynamic Programming approach:

动态编程方法:

    1)  Create a 2D array DP with size m, n
    2)  Initialize DP[0][0] =0 which is similar to step 1 in our previous function.
    3)  for i=1 to n-1
            DP[0][i]=1
        end for
        This is similar to step2 in our previous function
    4)  for i=1 to m-1
            DP[i][0]=1
        end for
        This is similar to step3 in our previous function

    5)  Compute the whole DP table
        for i=1 to m-1
            for j=1 to n-1
                DP[i][j]=DP[i-1][j]+DP[i][j-1]
            end for
        end for   
        This is similar to step 4 in our previous recursive function
    6)  Return DP[m-1][n-1] which is the result.

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

int uniqueRoute(int m,int n){
	int DP[m][n];

	memset(DP,0,sizeof(DP));

	//base cases
	DP[0][0]=0;

	for(int i=1;i<m;i++)
		DP[i][0]=1;

	for(int i=1;i<n;i++)
		DP[0][i]=1;
	
	//compute dp table
	for(int i=1;i<m;i++){
		for(int j=1;j<n;j++){
			DP[i][j]=DP[i-1][j]+DP[i][j-1];
		}
	}
	return DP[m-1][n-1];
}

int main()
{
	int t,n,m;

	cout<<"Enter number of testcases\n";
	cin>>t;

	for(int i=0;i<t;i++){
		cout<<"Enter grid size, m & n\n";
		cin>>m>>n;
		cout<<"Number of unique paths from topleft to bottom right: "<<uniqueRoute(m,n)<<endl;
	}

	return 0;
}

Output

输出量

Enter number of testcases
2
Enter grid size, m & n
3 3
Number of unique paths from topleft to bottom right: 6
Enter grid size, m & n
3 4
Number of unique paths from topleft to bottom right: 10


翻译自: https://www.includehelp.com/icp/number-of-unique-paths.aspx

r 求唯一路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值