给定一个大小为NXN的整数矩阵。任务是找到矩阵中递减路径的数量。您可以从任何单元格开始,也可以从(i,j)单元格开始,然后移动到(i + 1,j),(i – 1,j),(i,j + 1)和(i ,j – 1)单元格。
例子:
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
解决此问题的想法是使用动态编程。声明一个dp [] []数组,其中dp [i] [j]存储可以从像元(i,j)形成的递减路径数。因此,我们将定义一个递归函数,以使用参数(例如,i,j,当前单元格的行号和列号)评估递减路径的数量。从单元格(i,j)进行所有可能的移动,并对路径总数进行计数。首先,我们将检查函数是否已经计算出输入位置(i,j)的递减路径数。如果是,则返回值dp [i] [j],否则找到允许的四个方向上的递减序列号,然后返回该值。同时,我们还将存储中间单元格减少的数量。由于DP [i] [j]为每个像元存储减少路径的数量,因此DP [] []所有像元的总和将响应完整矩阵中减少路径的数量。
下面是上述方法的实现:
C++
// CPP program to count number
// of decreasing path in a matrix
#include
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);
}
}
// function 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);
}
}
// function 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
Python3
# Python3 program to count number
# of decreasing path in a matrix
MAX = 100
# Function that returns the number of
# decreasing paths from a cell(i, j)
def CountDecreasingPathsCell(mat, dp, n, x, y):
# checkinf if already calculated
if (dp[x][y] != -1):
return dp[x][y]
# all possible paths
delta = [[0, 1], [1, 0],
[-1, 0], [0, -1]]
newx, newy = 0, 0
# counts the total number of paths
ans = 1
# In all four allowed direction.
for i in range(4):
# 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 and newx < n and newy >= 0 and
newy < n and mat[newx][newy] < mat[x][y]):
ans += CountDecreasingPathsCell(mat, dp, n,
newx, newy)
# function that returns the answer
dp[x][y] = ans
return dp[x][y]
# Function that counts the total
# decreasing path in the matrix
def countDecreasingPathsMatrix(n,mat):
dp = []
# Initalising dp[][] to -1.
for i in range(n):
l = []
for j in range(n):
l.append(-1)
dp.append(l)
sum = 0
# Calculating number of decreasing
# path from each cell.
for i in range(n):
for j in range(n):
sum += CountDecreasingPathsCell(mat, dp,
n, i, j)
return sum
# Driver Code
n = 2
mat = [[1, 2], [1, 3]]
# function call that returns the
# count of decreasing paths in a matrix
print(countDecreasingPathsMatrix(n, mat))
# This code is contributed by SHUBHAMSINGH10
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);
}
}
// function 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
时间复杂度: O(N 2 )
辅助空间: O(N 2 )