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.
$result = $result * $N;
$total = factorial($N + $N);
$total1 = factorial($N);
return ($total / $total1) / $total1;
// Driver code
$N = 5;
echo “Ways = “, countWays($N);
// This code is contributed by ajit
Ways = 252
- 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 reach the end of matrix with non-zero AND value
- Number of ways to pair people
- Number of ways to cut a stick of length N into K pieces
- Total number of ways to place X and Y at n places such that no two X are together
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to make mobile lock pattern
- Count ways to spell a number with repeated digits
- Number of ways to arrange a word such that all vowels occur together
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Number of ways to form a heap with n distinct integers
- Number of ways to select a node from each connected component
- Number of ways to arrange a word such that no vowels occur together
- Number of ways to reach Nth floor by taking at-most K leaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.