📌  相关文章
📜  计算从矩阵的左上角到右下角的所有可能路径,而无需交叉对角线

📅  最后修改于: 2021-06-25 17:21:51             🧑  作者: Mango

给定表示矩阵大小的整数N ,任务是找到从矩阵的左上角到达右下角而不与矩阵对角线相交的可能方式的数量。来自矩阵的任何单元格(i,j)的可能移动为(i,j + 1) (向右)或(i + 1,j) (向下)。

例子:

方法:可以通过以下观察来解决问题:

  • 矩阵中允许的移动是向下或向右移动一个单元,而不穿过对角线。
  • 因此,在任何时候,向下移动的数量将始终大于或等于向右移动的数量。
  • 因此,这遵循加泰罗尼亚语数字的模式

因此,根据观察结果,问题减少到计算N加泰罗尼亚数。考虑只为上三角计算的路径,因为不允许交叉对角线。如果从单元格(0,0)(1,0)的移动将导致对角线的交叉。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to calculate
// Binomial Coefficient C(n, r)
int binCoff(int n, int r)
{
    int val = 1;
    int i;
    if (r > (n - r)) {
 
        // C(n, r) = C(n, n-r)
        r = (n - r);
    }
 
    for (i = 0; i < r; i++) {
 
        // [n * (n-1) *---* (n-r+1)] /
        // [r * (r-1) *----* 1]
        val *= (n - i);
        val /= (i + 1);
    }
    return val;
}
// Function to calcualte
// the total possible paths
int findWays(int n)
{
    // Update n to n - 1 as (N - 1)
    // catalan number is the result
    n--;
 
    int a, b, ans;
 
    // Stores 2nCn
    a = binCoff(2 * n, n);
 
    // Stores Nth Catalan
    // number
    b = a / (n + 1);
 
    // Stores the required
    // answer
    ans = b;
 
    return ans;
}
 
// Driver Code
int main()
{
 
    int n = 4;
    cout << findWays(n);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG {
 
    // Function to calculate
    // Binomial Coefficient C(n, r)
    static int binCoff(int n, int r)
    {
        int val = 1;
        int i;
        if (r > (n - r)) {
            // C(n, r) = C(n, n-r)
            r = (n - r);
        }
 
        for (i = 0; i < r; i++) {
            // [n * (n - 1) *---* (n - r + 1)] /
            // [r * (r - 1) *----* 1]
            val *= (n - i);
            val /= (i + 1);
        }
        return val;
    }
 
    // Function to calcualte
    // the total possible paths
    static int findWays(int n)
    {
        // Update n to n - 1 as (N - 1)
        // catalan number is the result
        n--;
 
        int a, b, ans;
 
        // Stores 2nCn
        a = binCoff(2 * n, n);
 
        // Stores Nth Catalan
        // number
        b = a / (n + 1);
 
        // Stores the required
        // answer
        ans = b;
 
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.print(findWays(n));
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 Program to implement
# the above approach
# Function to calculate
# Binomial Coefficient C(n, r)
def binCoff(n, r):
    val = 1
    if (r > (n - r)):
 
        # C(n, r) = C(n, n-r)
        r = (n - r)
 
    for i in range (r):
 
        # [n * (n-1) *---* (n-r + 1)] /
        # [r * (r-1) *----* 1]
        val *= (n - i)
        val //= (i + 1)
    return val
   
# Function to calcualte
# the total possible paths
def findWays(n):
 
    # Update n to n - 1
    n = n - 1
 
    # Stores 2nCn
    a = binCoff(2 * n, n)
 
    # Stores Nth Catalan
    # number
    b = a // (n + 1)
 
    # Stores the required
    # answer
    ans = b
 
    return ans
 
# Driver Code
if __name__ == "__main__":
   
    n = 4
    print(findWays(n))
 
# This code is contributed by Chitranayal


C#
// C# Program to implement
// the above approach
using System;
class GFG {
 
    // Function to calculate
    // Binomial Coefficient C(n, r)
    static int binCoff(int n, int r)
    {
        int val = 1;
        int i;
        if (r > (n - r)) {
            // C(n, r) = C(n, n-r)
            r = (n - r);
        }
 
        for (i = 0; i < r; i++) {
            // [n * (n - 1) *---* (n - r + 1)] /
            // [r * (r - 1) *----* 1]
            val *= (n - i);
            val /= (i + 1);
        }
        return val;
    }
 
    // Function to calcualte
    // the total possible paths
    static int findWays(int n)
    {
        // Update n to n - 1 as (N - 1)
        // catalan number is the result
        n--;
 
        int a, b, ans;
 
        // Stores 2nCn
        a = binCoff(2 * n, n);
 
        // Stores Nth Catalan
        // number
        b = a / (n + 1);
 
        // Stores the required
        // answer
        ans = 2 * b;
 
        return ans;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int n = 4;
        Console.Write(findWays(n));
    }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
5