📜  计算从一个点到 Origin 的路径

📅  最后修改于: 2021-09-17 06:56:07             🧑  作者: Mango

你站在一个点(n, m) 上,你想通过向左或向下的步骤去原点(0, 0) ,即从每个点你可以移动(n-1, m)( n, m-1) 。求从点到原点的路径数。

例子:

Input : 3 6
Output : Number of Paths 84

Input : 3 0
Output : Number of Paths 1

由于我们只能向下和向左移动,因此我们将为每个组合运行递归循环
可以采取的步骤。

// Recursive function to count number of paths
countPaths(n, m)
{
    // If we reach bottom or top left, we are
    // have only one way to reach (0, 0)
    if (n==0 || m==0)
        return 1;

    // Else count sum of both ways
    return (countPaths(n-1, m) + countPaths(n, m-1));
} 

下面是上述步骤的实现。

C++
// C++ program to count total number of
// paths from a point to origin
#include
using namespace std;
 
// Recursive function to count number of paths
int countPaths(int n, int m)
{
    // If we reach bottom or top left, we are
    // have only one way to reach (0, 0)
    if (n==0 || m==0)
        return 1;
 
    // Else count sum of both ways
    return (countPaths(n-1, m) + countPaths(n, m-1));
}
 
// Driver Code
int main()
{
    int n = 3, m = 2;
    cout << " Number of Paths " << countPaths(n, m);
    return 0;
}


Java
// Java program to count total number of
// paths from a point to origin
import java.io.*;
 
class GFG {
     
    // Recursive function to count number of paths
    static int countPaths(int n, int m)
    {
        // If we reach bottom or top left, we are
        // have only one way to reach (0, 0)
        if (n == 0 || m == 0)
            return 1;
     
        // Else count sum of both ways
        return (countPaths(n - 1, m) + countPaths(n, m - 1));
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 3, m = 2;
        System.out.println (" Number of Paths "
                            + countPaths(n, m));
         
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 program to count
# total number of
# paths from a point to origin
# Recursive function to
# count number of paths
def countPaths(n,m):
 
    # If we reach bottom
    # or top left, we are
    # have only one way to reach (0, 0)
    if (n==0 or m==0):
        return 1
  
    # Else count sum of both ways
    return (countPaths(n-1, m) + countPaths(n, m-1))
 
# Driver Code
n = 3
m = 2
print(" Number of Paths ", countPaths(n, m))
 
# This code is contributed
# by Azkia Anam.


C#
// C# program to count total number of
// paths from a point to origin
using System;
         
public class GFG {
     
    // Recursive function to count number
    // of paths
    static int countPaths(int n, int m)
    {
         
        // If we reach bottom or top left,
        // we are have only one way to
        // reach (0, 0)
        if (n == 0 || m == 0)
            return 1;
     
        // Else count sum of both ways
        return (countPaths(n - 1, m)
                 + countPaths(n, m - 1));
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 3, m = 2;
         
        Console.WriteLine (" Number of"
         + " Paths " + countPaths(n, m));
         
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
// C++ program to count total number of
// paths from a point to origin
#include
using namespace std;
 
// DP based function to count number of paths
int countPaths(int n, int m)
{
    int dp[n+1][m+1];
 
    // Fill entries in bottommost row and leftmost
    // columns
    for (int i=0; i<=n; i++)
      dp[i][0] = 1;
    for (int i=0; i<=m; i++)
      dp[0][i] = 1;
 
    // Fill DP in bottom up manner
    for (int i=1; i<=n; i++)
       for (int j=1; j<=m; j++)
          dp[i][j] = dp[i-1][j] + dp[i][j-1];
 
    return dp[n][m];
}
 
// Driver Code
int main()
{
    int n = 3, m = 2;
    cout << " Number of Paths " << countPaths(n, m);
    return 0;
}


Java
// Java program to count total number of
// paths from a point to origin
import java.io.*;
 
class GFG {
     
    // DP based function to count number of paths
    static int countPaths(int n, int m)
    {
        int dp[][] = new int[n + 1][m + 1];
     
        // Fill entries in bottommost row and leftmost
        // columns
        for (int i = 0; i <= n; i++)
            dp[i][0] = 1;
        for (int i = 0; i <= m; i++)
            dp[0][i] = 1;
     
        // Fill DP in bottom up manner
        for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
     
        return dp[n][m];
    }
     
    // Driver Code
    public static void main (String[] args) {
        int n = 3, m = 2;
        System.out.println(" Number of Paths "
                           + countPaths(n, m));
         
    }
}
 
// This code is contributed by vt_m


Python3
# Python3 program to count total
# number of paths from a po to origin
 
# Recursive function to count
# number of paths
def countPaths(n, m):
 
    # If we reach bottom or top
    # left, we are have only one
    # way to reach (0, 0)
    if (n == 0 or m == 0):
        return 1
 
    # Else count sum of both ways
    return (countPaths(n - 1, m) +
            countPaths(n, m - 1))
 
# Driver Code
n = 3
m = 2
print("Number of Paths",
       countPaths(n, m))
 
# This code is contributed by ash264


C#
// C# program to count total number of
// paths from a point to origin
using System;
         
public class GFG {
     
    // DP based function to count number
    // of paths
    static int countPaths(int n, int m)
    {
        int [,]dp = new int[n + 1,m + 1];
     
        // Fill entries in bottommost row
        // and leftmost columns
        for (int i = 0; i <= n; i++)
            dp[i,0] = 1;
        for (int i = 0; i <= m; i++)
            dp[0,i] = 1;
     
        // Fill DP in bottom up manner
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                dp[i,j] = dp[i - 1,j]
                         + dp[i,j - 1];
         
        return dp[n,m];
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 3, m = 2;
         
        Console.WriteLine(" Number of"
        + " Paths " + countPaths(n, m));
         
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
// C++ Program for above approach
#include 
#include 
using namespace std;
 
// Function to find
// binomial Coefficient
int binomialCoeff(int n, int k)
{
    int C[k+1];
    memset(C, 0, sizeof(C));
    C[0] = 1;
   
    // Constructing Pascal's Triangle
    for (int i = 1; i <= n; i++)
    {
        for (int j = min(i, k); j > 0; j--)
            C[j] = C[j] + C[j-1];
    }
    return C[k];
}
 
//Driver Code
int main()
{
    int n=3, m=2;
    cout<<"Number of Paths: "<<
                binomialCoeff(n+m,n)<


Java
// Java Program for above approach
import java.io.*;
import java.util.*;
 
class GFG
{
    static int min(int a,int b)
    {
        return a 0; j--)
                C[j] = C[j] + C[j-1];
        }
        return C[k];
    }
   
    // Driver Code
    public static void main (String[] args)
    {
        int n=3,m=2;
        System.out.println("Number of Paths: " +
                           binomialCoeff(n+m,n));
    }
}
//Contributed by Vismay_7


Python3
# Python3 program for above approach
def binomialCoeff(n,k):
     
    C = [0]*(k+1)
    C[0] = 1
     
    # Computing Pascal's Triangle
    for i in range(1, n + 1):
         
        j = min(i ,k)
        while (j > 0):
            C[j] = C[j] + C[j-1]
            j -= 1
   
    return C[k]
   
# Driver Code
n=3
m=2
print("Number of Paths:",binomialCoeff(n+m,n))
 
# Contributed by Vismay_7


C#
// C# program for above approach
using System;
 
class GFG{
     
// Function to find
// binomial Coefficient
static int binomialCoeff(int n, int k)
{
    int[] C = new int[k + 1];
    C[0] = 1;
    
    // Constructing Pascal's Triangle
    for(int i = 1; i <= n; i++)
    {
        for(int j = Math.Min(i, k); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
    return C[k];
}
 
// Driver code
static void Main()
{
    int n = 3, m = 2;
     
    Console.WriteLine("Number of Paths: " +
                      binomialCoeff(n + m, n));
}
}
 
// This code is contributed by divyesh072019


Javascript


输出
Number of Paths 10

我们可以使用动态规划,因为存在重叠的子问题。我们可以绘制递归树来查看重叠问题。例如,在 countPaths(4, 4) 的情况下,我们多次计算 countPaths(3, 3)。

C++

// C++ program to count total number of
// paths from a point to origin
#include
using namespace std;
 
// DP based function to count number of paths
int countPaths(int n, int m)
{
    int dp[n+1][m+1];
 
    // Fill entries in bottommost row and leftmost
    // columns
    for (int i=0; i<=n; i++)
      dp[i][0] = 1;
    for (int i=0; i<=m; i++)
      dp[0][i] = 1;
 
    // Fill DP in bottom up manner
    for (int i=1; i<=n; i++)
       for (int j=1; j<=m; j++)
          dp[i][j] = dp[i-1][j] + dp[i][j-1];
 
    return dp[n][m];
}
 
// Driver Code
int main()
{
    int n = 3, m = 2;
    cout << " Number of Paths " << countPaths(n, m);
    return 0;
}

Java

// Java program to count total number of
// paths from a point to origin
import java.io.*;
 
class GFG {
     
    // DP based function to count number of paths
    static int countPaths(int n, int m)
    {
        int dp[][] = new int[n + 1][m + 1];
     
        // Fill entries in bottommost row and leftmost
        // columns
        for (int i = 0; i <= n; i++)
            dp[i][0] = 1;
        for (int i = 0; i <= m; i++)
            dp[0][i] = 1;
     
        // Fill DP in bottom up manner
        for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
     
        return dp[n][m];
    }
     
    // Driver Code
    public static void main (String[] args) {
        int n = 3, m = 2;
        System.out.println(" Number of Paths "
                           + countPaths(n, m));
         
    }
}
 
// This code is contributed by vt_m

蟒蛇3

# Python3 program to count total
# number of paths from a po to origin
 
# Recursive function to count
# number of paths
def countPaths(n, m):
 
    # If we reach bottom or top
    # left, we are have only one
    # way to reach (0, 0)
    if (n == 0 or m == 0):
        return 1
 
    # Else count sum of both ways
    return (countPaths(n - 1, m) +
            countPaths(n, m - 1))
 
# Driver Code
n = 3
m = 2
print("Number of Paths",
       countPaths(n, m))
 
# This code is contributed by ash264

C#

// C# program to count total number of
// paths from a point to origin
using System;
         
public class GFG {
     
    // DP based function to count number
    // of paths
    static int countPaths(int n, int m)
    {
        int [,]dp = new int[n + 1,m + 1];
     
        // Fill entries in bottommost row
        // and leftmost columns
        for (int i = 0; i <= n; i++)
            dp[i,0] = 1;
        for (int i = 0; i <= m; i++)
            dp[0,i] = 1;
     
        // Fill DP in bottom up manner
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                dp[i,j] = dp[i - 1,j]
                         + dp[i,j - 1];
         
        return dp[n,m];
    }
     
    // Driver Code
    public static void Main ()
    {
        int n = 3, m = 2;
         
        Console.WriteLine(" Number of"
        + " Paths " + countPaths(n, m));
         
    }
}
 
// This code is contributed by Sam007.

PHP


Javascript


输出
Number of Paths 10

另一种方法:

使用 Pascal 的三角形方法,我们还通过计算n+m C n的值来解决问题。当您增加m的值并保持n的值不变时,可以将其观察为一种模式。
下面是上述方法的实现:

C++

// C++ Program for above approach
#include 
#include 
using namespace std;
 
// Function to find
// binomial Coefficient
int binomialCoeff(int n, int k)
{
    int C[k+1];
    memset(C, 0, sizeof(C));
    C[0] = 1;
   
    // Constructing Pascal's Triangle
    for (int i = 1; i <= n; i++)
    {
        for (int j = min(i, k); j > 0; j--)
            C[j] = C[j] + C[j-1];
    }
    return C[k];
}
 
//Driver Code
int main()
{
    int n=3, m=2;
    cout<<"Number of Paths: "<<
                binomialCoeff(n+m,n)<

Java

// Java Program for above approach
import java.io.*;
import java.util.*;
 
class GFG
{
    static int min(int a,int b)
    {
        return a 0; j--)
                C[j] = C[j] + C[j-1];
        }
        return C[k];
    }
   
    // Driver Code
    public static void main (String[] args)
    {
        int n=3,m=2;
        System.out.println("Number of Paths: " +
                           binomialCoeff(n+m,n));
    }
}
//Contributed by Vismay_7

蟒蛇3

# Python3 program for above approach
def binomialCoeff(n,k):
     
    C = [0]*(k+1)
    C[0] = 1
     
    # Computing Pascal's Triangle
    for i in range(1, n + 1):
         
        j = min(i ,k)
        while (j > 0):
            C[j] = C[j] + C[j-1]
            j -= 1
   
    return C[k]
   
# Driver Code
n=3
m=2
print("Number of Paths:",binomialCoeff(n+m,n))
 
# Contributed by Vismay_7

C#

// C# program for above approach
using System;
 
class GFG{
     
// Function to find
// binomial Coefficient
static int binomialCoeff(int n, int k)
{
    int[] C = new int[k + 1];
    C[0] = 1;
    
    // Constructing Pascal's Triangle
    for(int i = 1; i <= n; i++)
    {
        for(int j = Math.Min(i, k); j > 0; j--)
            C[j] = C[j] + C[j - 1];
    }
    return C[k];
}
 
// Driver code
static void Main()
{
    int n = 3, m = 2;
     
    Console.WriteLine("Number of Paths: " +
                      binomialCoeff(n + m, n));
}
}
 
// This code is contributed by divyesh072019

Javascript


输出
Number of Paths: 10

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程