Total number of decreasing paths in a matrix
Given a matrix of size N X N of integers. The task is to find the number of decreasing path in the matrix. You are allowed to start from any cell and from the cell (i, j), you are allowed to move to (i + 1, j), (i – 1, j), (i, j + 1) and (i, j – 1) cell.
Examples:
Input : m[][] = { { 1, 2 },
{ 1, 3 } }
Output : 8
Explanation : Decreasing paths are { 1 }, { 1 }, { 2 }, { 3 },
{ 2, 1 }, { 3, 1 }, { 3, 2 }, { 3, 2, 1 }
Input : m[][] = { { 1, 2, 3 },
{ 1, 3, 4 },
{ 1, 5, 6 } }
Output : 41
The idea to solve this problem is to use Dynamic Programming. Declare a dp[][] array, where dp[i][j] stores the number of decreasing path that can be formed from cell (i, j). So, we will define a recursive function to evaluate the number of decreasing path with parameters, say i, j, the row number and column number of the current cell. Make every possible move from the cell(i,j) and keep a count of the total number of paths. First, we will check in the function that the number of decreasing paths for input position (i, j) is already calculated or not. If yes, return the value dp[i][j] else find the number of decreasing sequence in allowed four directions and return the value. Meanwhile, we will also store the number of decreasing for intermediate cells. Since DP[i][j] stores the number of decreasing paths for every cell, so the summation of all the cells of DP[][] will answer to count of decreasing paths in the complete matrix.
Below is the implementation of the above approach:
C++
// CPP program to count number // of decreasing path in a matrix #include <bits/stdc++.h> using namespace std; #define MAX 100 // Function that returns the number of // decreasing paths from a cell(i, j) int CountDecreasingPathsCell(int mat[MAX][MAX], int dp[MAX][MAX], int n, int x, int y) { // checkinf if already calculated if (dp[x][y] != -1) return dp[x][y]; // all possible paths int delta[4][2] = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } }; int newx, newy; // counts the total number of paths int ans = 1; // In all four allowed direction. for (int i = 0; i < 4; i++) { // new co-ordinates newx = x + delta[i][0]; newy = y + delta[i][1]; // Checking if not going out of matrix and next // cell value is less than current cell value. if (newx >= 0 && newx < n && newy >= 0 && newy < n && mat[newx][newy] < mat[x][y]) { ans += CountDecreasingPathsCell(mat, dp, n, newx, newy); } } // fucntion that returns the answer return dp[x][y] = ans; } // Function that counts the total // decreasing path in the matrix int countDecreasingPathsMatrix(int n, int mat[MAX][MAX]) { int dp[MAX][MAX]; // Initalising dp[][] to -1. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = -1; int sum = 0; // Calculating number of decreasing path from each cell. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) sum += CountDecreasingPathsCell(mat, dp, n, i, j); return sum; } // Driver Code int main() { int n = 2; int mat[MAX][MAX] = { { 1, 2 }, { 1, 3 } }; // function call that returns the // count of decreasing paths in a matrix cout << countDecreasingPathsMatrix(n, mat) << endl; return 0; } |
Java
// Java program to count number // of decreasing path in a matrix import java.util.*; import java.lang.*; import java.io.*; class GFG { public static Scanner scn = new Scanner(System.in); // Function that returns the number of // decreasing paths from a cell(i, j) public static int CountDecreasingPathsCell(int mat[][], int dp[][], int n, int x, int y) { // checkinf if already calculated if (dp[x][y] != -1) return dp[x][y]; // all possible paths int delta[][] = { { 0, 1 }, { 1, 0 }, { -1, 0}, { 0, -1}}; int newx, newy; // counts the total // number of paths int ans = 1; // In all four allowed direction. for (int i = 0; i < 4; i++) { // new co-ordinates newx = x + delta[i][0]; newy = y + delta[i][1]; // Checking if not going out // of matrix and next cell // value is less than current // cell value. if (newx >= 0 && newx < n && newy >= 0 && newy < n && mat[newx][newy] < mat[x][y]) { ans += CountDecreasingPathsCell(mat, dp, n, newx, newy); } } // fucntion that // returns the answer return dp[x][y] = ans; } // Function that counts the total // decreasing path in the matrix public static int countDecreasingPathsMatrix(int n, int mat[][]) { int dp[][] = new int[n][n]; // Initalising dp[][] to -1. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = -1; int sum = 0; // Calculating number of // decreasing path from each cell. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) sum += CountDecreasingPathsCell(mat, dp, n, i, j); return sum; } // Driver Code public static void main(String[] args) { int n = 2; int mat[][]= {{1, 2}, {1, 3}}; // function call that returns the // count of decreasing paths in a matrix System.out.println(countDecreasingPathsMatrix(n, mat)); } } // This code is contributed by khyati grover |
C#
// C# program to count number // of decreasing path in a matrix using System; class GFG { // Function that returns // the number of decreasing // paths from a cell(i, j) public static int CountDecreasingPathsCell(int[,] mat, int[,] dp, int n, int x, int y) { // checkinf if already // calculated if (dp[x, y] != -1) return dp[x, y]; // all possible paths int[,] delta = {{0, 1}, {1, 0}, {-1, 0},{0, -1}}; int newx, newy; // counts the total // number of paths int ans = 1; // In all four // allowed direction. for (int i = 0; i < 4; i++) { // new co-ordinates newx = x + delta[i,0]; newy = y + delta[i,1]; // Checking if not going out // of matrix and next cell // value is less than current // cell value. if (newx >= 0 && newx < n && newy >= 0 && newy < n && mat[newx,newy] < mat[x,y]) { ans += CountDecreasingPathsCell(mat, dp, n, newx, newy); } } // fucntion that // returns the answer return dp[x,y] = ans; } // Function that counts the total // decreasing path in the matrix public static int countDecreasingPathsMatrix(int n, int[,] mat) { int[,] dp = new int[n, n]; // Initalising dp[][] to -1. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i, j] = -1; int sum = 0; // Calculating number of // decreasing path from each cell. for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) sum += CountDecreasingPathsCell(mat, dp, n, i, j); return sum; } // Driver code static public void Main () { int n = 2; int[,] mat= {{1, 2}, {1, 3}}; // function call that returns the // count of decreasing paths in a matrix Console.WriteLine(countDecreasingPathsMatrix(n, mat)); } } // This code is contributed by vij. |
8
Time Complexity : O(N2)
Auxiliary Space : O(N2)
Recommended Posts:
- Total number of non-decreasing numbers with n digits
- Number of palindromic paths in a matrix
- Maths behind number of paths in matrix problem
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Paths from entry to exit in matrix and maximum path sum
- Print all palindromic paths from top left to bottom right in a matrix
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Total coverage of all zeros in a binary matrix
- Number of n-digits non-decreasing integers
- Number of paths with exactly k coins
- Count number of paths with at-most k turns
- Number of Paths of Weight W in a K-ary tree
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Number of different cyclic paths of length N in a tetrahedron
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.



