Given the NxN grid of horizontal and vertical roads. The task is to find out the number of ways that the person can go from point A to point B using the shortest possible path.
Note: A and B point are fixed i.e A is at top left corner and B at bottom right corner as shown in the below image.
In the above image, the path shown in the red and light green colour are the two possible paths to reach from point A to point B.
Input: N = 3 Output: Ways = 20 Input: N = 4 Output: Ways = 70
Let the grid be N x N, number of ways can be written as.
How does above formula work?
Let consider the example of the 5×5 grid as shown above. In order to go from point A to point B in the 5×5 grid, We have to take 5 horizontal steps and 5 vertical steps. Each path will be an arrangement of 10 steps out of which 5 steps are identical of one kind and other 5 steps are identical of a second kind. Therefore
No. of ways = 10! / (5! * 5!) i.e 252 ways.
Ways = 252
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to pair people
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to select exactly K even numbers from given Array
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to sum up a total of N from limited denominations
- Total number of ways to place X and Y at n places such that no two X are together
- Number of ways to color N-K blocks using given operation
- Number of ways to convert a character X to a string Y
- Number of ways to cut a stick of length N into K pieces
- Number of ways to distribute N Paper Set among M students
- Number of ways to write N as a sum of K non-negative integers
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Count ways to spell a number with repeated digits
- Number of ways to make mobile lock pattern
- Number of ways to arrange a word such that no vowels occur together
- Number of ways of scoring R runs in B balls with at most W wickets
- Number of ways to reach (X, Y) in a matrix starting from the origin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.