Number of ways to go from one point to another in a grid Last Updated : 16 Apr, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.Examples: Input: N = 3 Output: Ways = 20 Input: N = 4 Output: Ways = 70 Formula: 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 5x5 grid as shown above. In order to go from point A to point B in the 5x5 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. ThereforeNo. of ways = 10! / (5! * 5!) i.e 252 ways. C++ // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // function that will // calculate the factorial long factorial(int N) { int result = 1; while (N > 0) { result = result * N; N--; } return result; } long countWays(int N) { long total = factorial(N + N); long total1 = factorial(N); return (total / total1) / total1; } // Driver code int main() { int N = 5; cout << "Ways = " << countWays(N); return 0; } Java // Java implementation of above approach class GfG { // function that will // calculate the factorial static long factorial(int N) { int result = 1; while (N > 0) { result = result * N; N--; } return result; } static long countWays(int N) { long total = factorial(N + N); long total1 = factorial(N); return (total / total1) / total1; } // Driver code public static void main(String[] args) { int N = 5; System.out.println("Ways = " + countWays(N)); } } Python3 # Python3 implementation of above approach # function that will calculate the factorial def factorial(N) : result = 1; while (N > 0) : result = result * N; N -= 1; return result; def countWays(N) : total = factorial(N + N); total1 = factorial(N); return (total // total1) // total1; # Driver code if __name__ == "__main__" : N = 5; print("Ways =", countWays(N)); # This code is contributed by Ryuga C# // C# implementation of above approach using System; class GfG { // function that will // calculate the factorial static long factorial(int N) { int result = 1; while (N > 0) { result = result * N; N--; } return result; } static long countWays(int N) { long total = factorial(N + N); long total1 = factorial(N); return (total / total1) / total1; } // Driver code public static void Main(String []args) { int N = 5; Console.WriteLine("Ways = " + countWays(N)); } } // This code is contributed by Arnab Kundu PHP <?php // PHP implementation of above approach // function that will // calculate the factorial function factorial($N) { $result = 1; while ($N > 0) { $result = $result * $N; $N--; } return $result; } function countWays($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 ?> JavaScript <script> // Javascript implementation of above approach // function that will // calculate the factorial function factorial(N) { var result = 1; while (N > 0) { result = result * N; N--; } return result; } function countWays(N) { var total = factorial(N + N); var total1 = factorial(N); return (total / total1) / total1; } // Driver code var N = 5; document.write( "Ways = " + countWays(N)); // This code is contributed by rutvik_56. </script> Output: Ways = 252 Comment More infoAdvertise with us Next Article Minimum steps needed to cover a sequence of points on an infinite grid N Naman_Garg Follow Improve Article Tags : Combinatorial DSA factorial Permutation and Combination binomial coefficient Marketing +2 More Practice Tags : Combinatorialfactorial Similar Reads Count number of ways to cover a distance | Set 2 Given a distance N. The task is to count the total number of ways to cover the distance with 1, 2 and 3 steps.Examples: Input: N = 3 Output: 4 All the required ways are (1 + 1 + 1), (1 + 2), (2 + 1) and (3).Input: N = 4 Output: 7 Approach: In previous article, a recursive and dynamic programming bas 5 min read Count number of ways to cover a distance | Set 2 Given a distance N. The task is to count the total number of ways to cover the distance with 1, 2 and 3 steps.Examples: Input: N = 3 Output: 4 All the required ways are (1 + 1 + 1), (1 + 2), (2 + 1) and (3).Input: N = 4 Output: 7 Approach: In previous article, a recursive and dynamic programming bas 5 min read Minimum steps needed to cover a sequence of points on an infinite grid Given an infinite grid, initial cell position (x, y) and a sequence of other cell position which needs to be covered in the given order. The task is to find the minimum number of steps needed to travel to all those cells. Note: Movement can be done in any of the eight possible directions from a give 7 min read Number of ways to make mobile lock pattern A mobile pattern is a grid of 3X3 cell, where drawing a specific pattern (connecting specific sequence of cells in order) will unlock the mobile. In this problem, the task is to calculate number of ways of making the lock pattern with number of connections in given range. In general terms, we are gi 11 min read Minimum number of steps required to reach origin from a given point Given two integers A and B representing coordinates of a point in the first quadrant, the task is to find the minimum number of steps required to reach the origin. All possible moves from a point (i, j) are (i - 1, j), (i, j - 1) or (i, j) (staying at the same position). Note: It is not allowed to m 6 min read Count number of ways to reach a given score in a Matrix Given a N x N matrix mat[][] consisting of non-negative integers, the task is to find the number of ways to reach a given score M starting from the cell (0, 0) and reaching the cell (N - 1, N - 1) by going only down(from (i, j) to (i + 1, j)) or right(from (i, j) to (i, j + 1)). Whenever a cell (i, 8 min read Like