📌  相关文章
📜  计算从矩阵左上角到右下角的所有可能路径而不穿过对角线

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

给定一个表示矩阵大小的整数N ,任务是找到从矩阵的左上角到达右下角而不穿过矩阵的对角线的可能方法的数量。矩阵中任何单元格(i, j)的可能移动是(i, j + 1) (Right) 或(i + 1, j) (Down)。

例子:

方法:该问题可以基于以下观察来解决:

  • 矩阵中允许的移动是向下或向右一个单元格而不跨越对角线。
  • 因此,在任何时候,向下移动的次数总是大于或等于向右移动的次数。
  • 因此,这遵循Catalan Numbers的模式

因此,根据观察,问题简化为计算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

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