Given four positive integers **M, N, X, and Y**, the task is to count all the possible ways to reach from top left(i.e., **(0, 0)**) to the bottom right **(M, N)** of a matrix of size **(M+1)x(N+1)** without visiting the cell **(X, Y)**. It is given that from each cell **(i, j)** you can either move only to right **(i, j + 1)** or down **(i + 1, j)**.**Examples:**

Input:M = 2, N = 2, X = 1, Y = 1Output:2Explanation:

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 = 2Output:66Explanation:

There are 66 ways to reach (5, 4) without visiting (3, 2).

**Approach:**

To solve the problem mentioned above the idea is to subtract the number of ways to reach from **(0, 0)** to **(X, Y)** which was followed by reaching **(M, N)** from **(X, Y)** by visiting **(X, Y)** from the total number of ways reaching **(M, N)** from **(0, 0)**.

Therefore,

- The number of ways to reach from
**(M, N)**from the origin**(0, 0)**is given by:

- The number of ways to reach
**(M, N)**only by visiting**(X, Y)**is reaching**(X, Y)**from**(0, 0)**which was followed by reaching**(M, N)**from**(X, Y)**is given by:

Therefore,

Hence, the equation for the total number of ways are:

Below is the implementation of the above approach:

## C++

`// C++ program from the above approach ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

66

## Recommended Posts:

- Number of ways to reach (X, Y) in a matrix starting from the origin
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Number of ways to reach the end of matrix with non-zero AND value
- Count number of ways to reach a given score in a Matrix
- Count of ways to traverse a Matrix and return to origin in K steps
- Minimum number of moves to reach N starting from (1, 1)
- Count number of ways to reach destination in a maze
- Count number of ways to reach destination in a Maze
- Count number of ways to reach destination in a Maze using BFS
- Number of ways to reach Nth floor by taking at-most K leaps
- Counts paths from a point to reach Origin
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Find the number of ways to reach Kth step in stair case
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Number of ways to make exactly C components in a 2*N matrix
- Count the number of ways to traverse a Matrix
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Count ways to reach the n'th stair
- Count ways to reach the nth stair using step 1, 2 or 3
- Print a matrix in a spiral form starting from a point

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.