给定三个整数N 、 M和K ,其中N和M是矩阵的维度,K 是最大可能的步长,任务是计算从 (0, 0) 开始并返回遍历矩阵的方法的数量,使用仅K步。
注意:每一步都可以向上、向下、向左、向右移动或停留在当前位置。答案可能很大,所以打印答案模10 9 + 7
例子:
Input: N = 2, M = 2, K = 2
Output: 3
Explanation:
Three ways are:
1)Stay, Stay.
2)Up, Down
3)Right, Left
Input: N = 3, M = 3, K = 4
Output: 23
方法:该问题也可以使用 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
输出:
23
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live