📜  遍历矩阵并以 K 步返回原点的方法计数

📅  最后修改于: 2021-09-17 07:04:21             🧑  作者: Mango

给定三个整数NMK ,其中NM是矩阵的维度,K 是最大可能的步长,任务是计算从 (0, 0) 开始并返回遍历矩阵的方法的数量,使用仅K步。
注意:每一步都可以向上、向下、向左、向右移动或停留在当前位置。答案可能很大,所以打印答案模10 9 + 7
例子:

方法:该问题也可以使用 Memoization 技术解决。请按照以下步骤解决问题:

  • 从位置 (0, 0) 开始,递归调用每个可能的位置并将步长值减 1。
  • 创建一个大小为 N * M * K ( DP[N][M][S] ) 的 3D 数组
Possible ways for each step
can be calculated by:

 DP[i][j][k]
  =  Recursion(i+1, j, k-1)  +
     Recursion(i-1, j, k-1)  +  
     Recursion(i, j-1, k-1)  +  
     Recursion(i, j+1, k-1)  +  
     Recursion(i, j, k-1).

With base condition returning 0,
whenever
i >= l or i = b or j < 0 or k < 0.

下面是上述方法的实现:

C++
// C++ program to count total
// number of ways to return
// to origin after completing
// given number of steps.
 
#include 
using namespace std;
#define MOD 1000000007
 
long long dp[101][101][101];
int N, M, K;
 
// Function Initialize dp[][][]
// array with -1
void Initialize()
{
 
    for (int i = 0; i <= 100; i++)
        for (int j = 0; j <= 100; j++)
            for (int z = 0; z <= 100; z++)
                dp[i][j][z] = -1;
}
 
// Function returns the total count
int CountWays(int i, int j, int k)
{
    if (i >= N || i < 0
        || j >= M || j < 0 || k < 0)
        return 0;
 
    if (i == 0 && j == 0
        && k == 0)
        return 1;
 
    if (dp[i][j][k] != -1)
        return dp[i][j][k];
    else
        dp[i][j][k]
            = (CountWays(i + 1, j, k - 1) % MOD
               + CountWays(i - 1, j, k - 1) % MOD
               + CountWays(i, j - 1, k - 1) % MOD
               + CountWays(i, j + 1, k - 1) % MOD
               + CountWays(i, j, k - 1) % MOD)
              % MOD;
 
    return dp[i][j][k];
}
 
// Driver Program
int main()
{
    N = 3;
    M = 3;
    K = 4;
 
    Initialize();
    cout << CountWays(0, 0, K)
         << "\n";
 
    return 0;
}


Java
// Java program to count total
// number of ways to return
// to origin after completing
// given number of steps.
class GFG{
     
static int [][][] dp = new int[101][101][101];
static int N, M, K;
static int MOD = 1000000007;
     
// Function Initialize dp[][][]
// array with -1
public static void Initialize()
{
    for(int i = 0; i <= 100; i++)
       for(int j = 0; j <= 100; j++)
          for(int z = 0; z <= 100; z++)
             dp[i][j][z] = -1;
}
     
// Function returns the total count
public static int CountWays(int i, int j, int k)
{
    if (i >= N || i < 0 ||
        j >= M || j < 0 || k < 0)
        return 0;
     
    if (i == 0 && j == 0 && k == 0)
        return 1;
     
    if (dp[i][j][k] != -1)
        return dp[i][j][k];
     
    else
        dp[i][j][k] = (CountWays(i + 1, j, k - 1) % MOD +
                       CountWays(i - 1, j, k - 1) % MOD +
                       CountWays(i, j - 1, k - 1) % MOD +
                       CountWays(i, j + 1, k - 1) % MOD +
                       CountWays(i, j, k - 1) % MOD) % MOD;
     
    return dp[i][j][k];
}
 
// Driver code
public static void main(String[] args)
{
    N = 3;
    M = 3;
    K = 4;
     
    Initialize();
    System.out.println(CountWays(0, 0, K));
}
}
 
// This code is contributed by grand_master


Python3
# Python3 program to count total
# number of ways to return
# to origin after completing
# given number of steps.
MOD = 1000000007
 
dp = [[[0 for i in range(101)]
          for i in range(101)]
          for i in range(101)]
N, M, K = 0, 0, 0
 
# Function Initialize dp[][][]
# array with -1
def Initialize():
 
    for i in range(101):
        for j in range(101):
            for z in range(101):
                dp[i][j][z] = -1
 
# Function returns the total count
def CountWays(i, j, k):
 
    if (i >= N or i < 0 or
        j >= M or j < 0 or k < 0):
        return 0
 
    if (i == 0 and j == 0 and k == 0):
        return 1
 
    if (dp[i][j][k] != -1):
        return dp[i][j][k]
    else:
        dp[i][j][k] = (CountWays(i + 1, j, k - 1) % MOD +
                       CountWays(i - 1, j, k - 1) % MOD +
                       CountWays(i, j - 1, k - 1) % MOD +
                       CountWays(i, j + 1, k - 1) % MOD +
                       CountWays(i, j, k - 1) % MOD) % MOD
    return dp[i][j][k]
 
# Driver code
if __name__ == '__main__':
     
    N = 3
    M = 3
    K = 4
 
    Initialize()
    print(CountWays(0, 0, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program to count total
// number of ways to return
// to origin after completing
// given number of steps.
using System;
 
class GFG{
     
static int [,,] dp = new int[101, 101, 101];
static int N, M, K;
static int MOD = 1000000007;
     
// Function Initialize [,]dp[]
// array with -1
public static void Initialize()
{
    for(int i = 0; i <= 100; i++)
        for(int j = 0; j <= 100; j++)
            for(int z = 0; z <= 100; z++)
                dp[i, j, z] = -1;
}
     
// Function returns the total count
public static int CountWays(int i, int j, int k)
{
    if (i >= N || i < 0 ||
        j >= M || j < 0 || k < 0)
        return 0;
     
    if (i == 0 && j == 0 && k == 0)
        return 1;
     
    if (dp[i, j, k] != -1)
        return dp[i, j, k];
     
    else
        dp[i, j, k] = (CountWays(i + 1, j, k - 1) % MOD +
                       CountWays(i - 1, j, k - 1) % MOD +
                       CountWays(i, j - 1, k - 1) % MOD +
                       CountWays(i, j + 1, k - 1) % MOD +
                       CountWays(i, j, k - 1) % MOD) % MOD;
     
    return dp[i, j, k];
}
 
// Driver code
public static void Main(String[] args)
{
    N = 3;
    M = 3;
    K = 4;
     
    Initialize();
    Console.WriteLine(CountWays(0, 0, K));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
23