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

📅  最后修改于: 2021-06-25 15:12:34             🧑  作者: 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现场课程》和《 Geeks现场课程美国》。