Count of all unique paths from given source to destination in a Matrix - GeeksforGeeks

16 Dec, 2021
Given a 2D matrix of size n*m, a source ‘s’ and a destination ‘d’, print the count of all unique paths from given ‘s’ to ‘d’.  From each cell, you can either move only to the right or down.
Examples:
Input: arr[][] = { {1, 2, 3}, {4, 5, 6} }, s = {0, 0}, d = {1, 2}Output: 3Explanation: All possible paths from source to destination are:
1 -> 4 -> 5 -> 6
1 -> 2 -> 5 -> 6
1 -> 2 -> 3 -> 6
Input: arr[][] = { {1, 2}, {3, 4} }, s = {0, 1}, d = {1, 1}Output: 1
Approach: Use recursion to move right first & then down from each cell in the path of the matrix, starting from the source. If the destination is reached, increment the count of possible paths.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the// count of all possible pathsint countPaths(int i, int j, int count,               int p, int q){    // Destination is reached    if (i == p || j == q) {        count++;        return count;    }     // Move right    count = countPaths(i, j + 1,                       count, p, q);     // Move down    count = countPaths(i + 1, j,                       count, p, q);    return count;} // Driver program to test above functionsint main(){    vector<vector<int> > mat = { { 1, 2, 3 },                                 { 4, 5, 6 } };    vector<int> s = { 0, 0 };    vector<int> d = { 1, 2 };    cout << countPaths(s[0], s[1], 0,                       d[0], d[1]);    return 0;}
// Java program for the above approachclass GFG {     // Function to find the    // count of all possible paths    static int countPaths(int i, int j, int count,            int p, int q) {               // Destination is reached        if (i == p || j == q) {            count++;            return count;        }         // Move right        count = countPaths(i, j + 1,                count, p, q);         // Move down        count = countPaths(i + 1, j,                count, p, q);        return count;    }     // Driver program to test above functions    public static void main(String args[]) {        int[] s = { 0, 0 };        int[] d = { 1, 2 };        System.out.println(countPaths(s[0], s[1], 0, d[0], d[1]));    }} // This code is contributed by gfgking.
# python program for the above approach # Function to find the# count of all possible pathsdef countPaths(i, j, count, p, q):     # Destination is reached    if (i == p or j == q):        count += 1        return count     # Move right    count = countPaths(i, j + 1, count, p, q)     # Move down    count = countPaths(i + 1, j, count, p, q)    return count # Driver program to test above functionsif __name__ == "__main__":     mat = [[1, 2, 3],           [4, 5, 6]]    s = [0, 0]    d = [1, 2]    print(countPaths(s[0], s[1], 0, d[0], d[1]))     # This code is contributed by rakeshsahni
// C# program for the above approach using System;class GFG {     // Function to find the    // count of all possible paths    static int countPaths(int i, int j, int count, int p, int q) {               // Destination is reached        if (i == p || j == q) {            count++;            return count;        }         // Move right        count = countPaths(i, j + 1,                count, p, q);         // Move down        count = countPaths(i + 1, j,                count, p, q);        return count;    }     // Driver program to test above functions    public static void Main() {        int[] s = { 0, 0 };        int[] d = { 1, 2 };        Console.Write(countPaths(s[0], s[1], 0, d[0], d[1]));    }} // This code is contributed by gfgking.
<script>       // JavaScript code for the above approach        // Function to find the       // count of all possible paths       function countPaths(i, j, count,           p, q)       {                   // Destination is reached           if (i == p || j == q) {               count++;               return count;           }            // Move right           count = countPaths(i, j + 1,               count, p, q);            // Move down           count = countPaths(i + 1, j,               count, p, q);           return count;       }        // Driver program to test above functions       let mat = [[1, 2, 3],                   [4, 5, 6]];       let s = [0, 0];       let d = [1, 2];       document.write(countPaths(s[0], s[1], 0,           d[0], d[1]));  // This code is contributed by Potta Lokesh   </script>
  
3
 
Time Complexity: O(n+m)Auxiliary Space: O(1)
 
lokeshpotta20
rakeshsahni
gfgking
Backtracking
Matrix
Recursion
Recursion
Matrix
Backtracking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Tug of War
Difference between Backtracking and Branch-N-Bound technique
Find if there is a path of more than k length from a source
N-Queen Problem | Local Search using Hill climbing with random neighbour
Find shortest safe route in a path with landmines
Matrix Chain Multiplication | DP-8
Program to find largest element in an array
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Print a given matrix in spiral form
Find the number of islands | Set 1 (Using DFS)