📌  相关文章
📜  通过向右、向下或对角线移动从 M x N 矩阵的左上角到右下角的可能路径计数

📅  最后修改于: 2022-05-13 01:56:09.102000             🧑  作者: Mango

通过向右、向下或对角线移动从 M x N 矩阵的左上角到右下角的可能路径计数

给定 2 个整数MN,任务是找到M x N矩阵的从左上角到右下角的所有可能路径的计数,约束条件是每个单元格只能向右或向下或对角线移动

例子:

方法:这个想法是使用递归来找到路径的总数。这种方法与本文中讨论的方法非常相似。请按照以下步骤解决问题:

  • 如果MN等于1,则返回1。
  • 否则,创建一个递归函数numberOfPaths() 为分别表示垂直、水平和对角线移动的值{M-1, N}、{M, N-1}{M-1, N-1}调用相同的函数。

下面是上述方法的实现:

C++
// C++  program for the above approach
#include 
using namespace std;
 
// Returns count of possible paths to
// reach cell at row number M and column
// number N from the topmost leftmost
// cell (cell at 1, 1)
int numberOfPaths(int M, int N)
{
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
        return 1;
 
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    return numberOfPaths(M - 1, N)
          + numberOfPaths(M, N - 1)
           + numberOfPaths(M - 1, N - 1);
}
 
// Driver Code
int main()
{
    cout << numberOfPaths(3, 3);
    return 0;
}


Java
// Java  program for the above approach
import java.util.*;
public class GFG
{
   
// Returns count of possible paths to
// reach cell at row number M and column
// number N from the topmost leftmost
// cell (cell at 1, 1)
static int numberOfPaths(int M, int N)
{
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
        return 1;
 
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    return numberOfPaths(M - 1, N)
          + numberOfPaths(M, N - 1)
           + numberOfPaths(M - 1, N - 1);
}
 
// Driver Code
public static void main(String args[])
{
    System.out.print(numberOfPaths(3, 3));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python
# Python  program for the above approach
 
# Returns count of possible paths to
# reach cell at row number M and column
# number N from the topmost leftmost
# cell (cell at 1, 1)
def numberOfPaths(M, N):
 
    # If either given row number or
    # given column number is first
    if (M == 1 or N == 1):
        return 1
 
    # Horizontal Paths +
    # Vertical Paths +
    # Diagonal Paths
    return numberOfPaths(M - 1, N) \
        + numberOfPaths(M, N - 1) \
        + numberOfPaths(M - 1, N - 1)
 
# Driver Code
print(numberOfPaths(3, 3))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C#  program for the above approach
using System;
 
public class GFG
{
   
// Returns count of possible paths to
// reach cell at row number M and column
// number N from the topmost leftmost
// cell (cell at 1, 1)
static int numberOfPaths(int M, int N)
{
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
        return 1;
 
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    return numberOfPaths(M - 1, N)
          + numberOfPaths(M, N - 1)
           + numberOfPaths(M - 1, N - 1);
}
 
// Driver Code
public static void Main(String []args)
{
    Console.Write(numberOfPaths(3, 3));
 
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
static int dp[1001][1001];
 
// Returns count of possible paths to
// reach cell at row number M and column
// number N from the topmost leftmost
// cell (cell at 1, 1)
int numberOfPaths(int M, int N)
{
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
        return 1;
     
    // If a value already present
    // in t[][], return it
    if(dp[M][N] != -1) {
        return dp[M][N];
    }
     
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    return dp[M][N] = numberOfPaths(M - 1, N)
                    + numberOfPaths(M, N - 1)
                    + numberOfPaths(M - 1, N - 1);
}
 
// Driver Code
int main()
{
    memset(dp,-1,sizeof(dp));
    cout << numberOfPaths(3, 3);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG {
  static int[][] dp = new int[1001][1001];
 
  // Returns count of possible paths to
  // reach cell at row number M and column
  // number N from the topmost leftmost
  // cell (cell at 1, 1)
  static int numberOfPaths(int M, int N)
  {
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
      return 1;
 
    // If a value already present
    // in t[][], return it
    if (dp[M][N] != -1) {
      return dp[M][N];
    }
 
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    dp[M][N] = numberOfPaths(M - 1, N)
      + numberOfPaths(M, N - 1)
      + numberOfPaths(M - 1, N - 1);
    return dp[M][N];
  }
 
  // Driver Code
  public static void main(String args[])
  {
    for (int i = 0; i < 1001; i++)
      for (int j = 0; j < 1001; j++)
        dp[i][j] = -1;
    System.out.println(numberOfPaths(3, 3));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python
# Python program for the above approach
 
# Taking the matrix as globally
dp = [[-1 for i in range(1001)] for j in range(1001)]
 
# Returns count of possible paths to
# reach cell at row number M and column
# number N from the topmost leftmost
# cell (cell at 1, 1)
def numberOfPaths(M, N):
 
    # If either given row number or
    # given column number is first
    if (M == 1 or N == 1):
        return 1
 
    # If a value already present
    # in t[][], return it
    if(dp[M][N] != -1):
        return dp[M][N]
 
    # Horizontal Paths +
    # Vertical Paths +
    # Diagonal Paths
    dp[M][N] = numberOfPaths(M - 1, N) + numberOfPaths(M,
                     N - 1) + numberOfPaths(M - 1, N - 1)
    return dp[M][N]
 
# Driver Code
print(numberOfPaths(3, 3))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
class GFG {
    static int[, ] dp = new int[1001, 1001];
 
    // Returns count of possible paths to
    // reach cell at row number M and column
    // number N from the topmost leftmost
    // cell (cell at 1, 1)
    static int numberOfPaths(int M, int N)
    {
 
        // If either given row number or
        // given column number is first
        if (M == 1 || N == 1)
            return 1;
 
        // If a value already present
        // in t[][], return it
        if (dp[M, N] != -1) {
            return dp[M, N];
        }
 
        // Horizontal Paths +
        // Vertical Paths +
        // Diagonal Paths
        dp[M, N] = numberOfPaths(M - 1, N)
                   + numberOfPaths(M, N - 1)
                   + numberOfPaths(M - 1, N - 1);
        return dp[M, N];
    }
 
    // Driver Code
    public static void Main()
    {
        for (int i = 0; i < 1001; i++)
            for (int j = 0; j < 1001; j++)
                dp[i, j] = -1;
        Console.Write(numberOfPaths(3, 3));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
13

时间复杂度: O(3 M*N )
辅助空间: O(1)

有效方法Dp(使用记忆)

C++

// C++ program for the above approach
#include 
using namespace std;
 
static int dp[1001][1001];
 
// Returns count of possible paths to
// reach cell at row number M and column
// number N from the topmost leftmost
// cell (cell at 1, 1)
int numberOfPaths(int M, int N)
{
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
        return 1;
     
    // If a value already present
    // in t[][], return it
    if(dp[M][N] != -1) {
        return dp[M][N];
    }
     
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    return dp[M][N] = numberOfPaths(M - 1, N)
                    + numberOfPaths(M, N - 1)
                    + numberOfPaths(M - 1, N - 1);
}
 
// Driver Code
int main()
{
    memset(dp,-1,sizeof(dp));
    cout << numberOfPaths(3, 3);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
public class GFG {
  static int[][] dp = new int[1001][1001];
 
  // Returns count of possible paths to
  // reach cell at row number M and column
  // number N from the topmost leftmost
  // cell (cell at 1, 1)
  static int numberOfPaths(int M, int N)
  {
 
    // If either given row number or
    // given column number is first
    if (M == 1 || N == 1)
      return 1;
 
    // If a value already present
    // in t[][], return it
    if (dp[M][N] != -1) {
      return dp[M][N];
    }
 
    // Horizontal Paths +
    // Vertical Paths +
    // Diagonal Paths
    dp[M][N] = numberOfPaths(M - 1, N)
      + numberOfPaths(M, N - 1)
      + numberOfPaths(M - 1, N - 1);
    return dp[M][N];
  }
 
  // Driver Code
  public static void main(String args[])
  {
    for (int i = 0; i < 1001; i++)
      for (int j = 0; j < 1001; j++)
        dp[i][j] = -1;
    System.out.println(numberOfPaths(3, 3));
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Python

# Python program for the above approach
 
# Taking the matrix as globally
dp = [[-1 for i in range(1001)] for j in range(1001)]
 
# Returns count of possible paths to
# reach cell at row number M and column
# number N from the topmost leftmost
# cell (cell at 1, 1)
def numberOfPaths(M, N):
 
    # If either given row number or
    # given column number is first
    if (M == 1 or N == 1):
        return 1
 
    # If a value already present
    # in t[][], return it
    if(dp[M][N] != -1):
        return dp[M][N]
 
    # Horizontal Paths +
    # Vertical Paths +
    # Diagonal Paths
    dp[M][N] = numberOfPaths(M - 1, N) + numberOfPaths(M,
                     N - 1) + numberOfPaths(M - 1, N - 1)
    return dp[M][N]
 
# Driver Code
print(numberOfPaths(3, 3))
 
# This code is contributed by Samim Hossain Mondal.

C#

// C# program for the above approach
using System;
class GFG {
    static int[, ] dp = new int[1001, 1001];
 
    // Returns count of possible paths to
    // reach cell at row number M and column
    // number N from the topmost leftmost
    // cell (cell at 1, 1)
    static int numberOfPaths(int M, int N)
    {
 
        // If either given row number or
        // given column number is first
        if (M == 1 || N == 1)
            return 1;
 
        // If a value already present
        // in t[][], return it
        if (dp[M, N] != -1) {
            return dp[M, N];
        }
 
        // Horizontal Paths +
        // Vertical Paths +
        // Diagonal Paths
        dp[M, N] = numberOfPaths(M - 1, N)
                   + numberOfPaths(M, N - 1)
                   + numberOfPaths(M - 1, N - 1);
        return dp[M, N];
    }
 
    // Driver Code
    public static void Main()
    {
        for (int i = 0; i < 1001; i++)
            for (int j = 0; j < 1001; j++)
                dp[i, j] = -1;
        Console.Write(numberOfPaths(3, 3));
    }
}
 
// This code is contributed by ukasp.

Javascript


输出
13

时间复杂度:O(M*N)

辅助空间:O(M*N)