给定四个正整数M, N, X, 和 Y ,任务是计算从左上角(即(0, 0) )到右下角(M, N)的所有可能的路径大小的矩阵(M+1)x(N+1)而不访问单元格(X, Y) 。假设从每个单元格(i, j) 中,您只能向右移动(i, j + 1)或向下移动(i + 1, j) 。
例子:
Input: M = 2, N = 2, X = 1, Y = 1
Output: 2
Explanation:
There are only 2 ways to reach (2, 2) without visiting (1, 1) and the two paths are:
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2)
(0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2)
Input: M = 5, N = 4, X = 3, Y = 2
Output: 66
Explanation:
There are 66 ways to reach (5, 4) without visiting (3, 2).
方法:
为了解决上面提到的问题,想法是减去从(0, 0)到(X, Y)的路径数量,然后通过访问(X, Y)从(X, Y)到达(M, N) Y)从(0, 0)到达(M, N)的方式总数。
所以,
- 从原点(0, 0)到(M, N)的路径数由下式给出:
- 仅通过访问(X, Y)到达(M, N)的方式数是从(0, 0)到达(X, Y)然后从(X, Y)到达(M, N)给出经过:
Therefore,
- 因此,总路数的等式为:
下面是上述方法的实现:
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