📌  相关文章
📜  在矩阵中从原点开始到达 (M, N) 而不访问 (X, Y) 的方法数

📅  最后修改于: 2021-09-06 06:09:10             🧑  作者: Mango

给定四个正整数M, N, X, 和 Y ,任务是计算从左上角(即(0, 0) )到右下角(M, N)的所有可能的路径大小的矩阵(M+1)x(N+1)而不访问单元格(X, Y) 。假设从每个单元格(i, j) 中,您只能向右移动(i, j + 1)或向下移动(i + 1, j)
例子:

方法:
为了解决上面提到的问题,想法是减去从(0, 0)(X, Y)的路径数量,然后通过访问(X, Y)(X, Y)到达(M, N) Y)(0, 0)到达(M, N)的方式总数。
所以,

  1. 从原点(0, 0)(M, N)的路径数由下式给出:
  1. 仅通过访问(X, Y)到达(M, N)的方式数是从(0, 0)到达(X, Y)然后从(X, Y)到达(M, N)给出经过:
  1. 因此,总路数的等式为:

下面是上述方法的实现:

C++
// C++ program from the above approach
#include 
using namespace std;
 
int fact(int n);
 
// Function for computing nCr
int nCr(int n, int r)
{
    return fact(n)
           / (fact(r) * fact(n - r));
}
 
// Function to find factorial of a number
int fact(int n)
{
    int res = 1;
 
    for (int i = 2; i <= n; i++)
        res = res * i;
 
    return res;
}
 
// Function for counting the number
// of ways to reach (m, n) without
// visiting (x, y)
int countWays(int m, int n, int x, int y)
{
    return nCr(m + n, m)
           - nCr(x + y, x) * nCr(m + n
                                     - x - y,
                                 m - x);
}
 
// Driver Code
int main()
{
    // Given Dimensions of Matrix
    int m = 5;
    int n = 4;
 
    // Cell not to be visited
    int x = 3;
    int y = 2;
 
    // Function Call
    cout << countWays(m, n, x, y);
    return 0;
}


Java
// Java program from the above approach    
import java.util.*;    
class GFG{   
     
// Function for computing nCr    
public static int nCr(int n, int r)        
{    
    return fact(n) / (fact(r) * fact(n - r));        
}    
         
// Function to find factorial of a number    
public static int fact(int n)    
{    
    int res = 1;
     
    for(int i = 2; i <= n; i++)        
        res = res * i;        
    return res;        
}    
         
// Function for counting the number        
// of ways to reach (m, n) without        
// visiting (x, y)        
public static int countWays(int m, int n,
                            int x, int y)        
{    
    return nCr(m + n, m) -
           nCr(x + y, x) *
           nCr(m + n - x - y, m - x);        
}
 
// Driver code
public static void main(String[] args)
{    
     
    // Given Dimensions of Matrix    
    int m = 5;        
    int n = 4;        
             
    // Cell not to be visited    
    int x = 3;        
    int y = 2;        
             
    // Function Call    
    System.out.println(countWays(m, n, x, y));    
}    
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function for computing nCr
def nCr(n, r):
     
    return (fact(n) // (fact(r) *
                        fact(n - r)))
 
# Function to find factorial of a number
def fact(n):
     
    res = 1
    for i in range(2, n + 1):
        res = res * i
 
    return res
 
# Function for counting the number
# of ways to reach (m, n) without
# visiting (x, y)
def countWays(m, n, x, y):
     
    return (nCr(m + n, m) - nCr(x + y, x) *
            nCr(m + n - x - y, m - x))
 
# Driver Code
 
# Given dimensions of Matrix
m = 5
n = 4
 
# Cell not to be visited
x = 3
y = 2
 
# Function call
print(countWays(m, n, x, y))
 
# This code is contributed by sanjoy_62


C#
// C# program from the above approach    
using System;
 
class GFG{
     
// Function for computing nCr    
public static int nCr(int n, int r)        
{    
    return fact(n) / (fact(r) * fact(n - r));        
}    
         
// Function to find factorial of a number    
public static int fact(int n)    
{    
    int res = 1;
     
    for(int i = 2; i <= n; i++)        
        res = res * i;
         
    return res;        
}    
         
// Function for counting the number        
// of ways to reach (m, n) without        
// visiting (x, y)        
public static int countWays(int m, int n,
                            int x, int y)        
{    
    return nCr(m + n, m) -
           nCr(x + y, x) *
           nCr(m + n - x - y, m - x);        
}
 
// Driver code
public static void Main(String[] args)
{    
     
    // Given dimensions of Matrix    
    int m = 5;        
    int n = 4;        
             
    // Cell not to be visited    
    int x = 3;        
    int y = 2;        
             
    // Function call    
    Console.WriteLine(countWays(m, n, x, y));    
}    
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
66

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live