给定两个数字N和M。任务是查找从左下角开始移动时到达N×M大小的网格中的像元(i,j)的最短路径数
注意: cell(i,j)表示网格中的第i行和第j列
下图显示了到达4×4网格中的cell(1,4)的最短路径
例子 :
Input : N = 3, M = 4
Output : 1 3 6 10
1 2 3 4
1 1 1 1
Input : N = 5, M = 2
Output : 1 5
1 4
1 3
1 2
1 1
方法:一种有效的方法是从左下角开始计算网格。
- 到达像元(n,i)的最短路径数为1,其中1 <= i <= M
- 到达像元(i,1)的最短路径数为1,其中1 <= i <= N
- 到达像元(i,j)的最短路径数是像元(i-1,j)和(i,j + 1)的最短路径数之和,其中1 <= j <= M和1 <=我<= N
下面是上述方法的实现:
C++
// CPP program to find number of shortest paths
#include
using namespace std;
// Function to find number of shortest paths
void NumberOfShortestPaths(int n, int m)
{
int a[n][m];
for (int i = 0; i < n; i++)
memset(a[i], 0, sizeof(a[i]));
// Compute the grid starting from
// the bottom-left corner
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
if (j == 0 or i == n - 1)
a[i][j] = 1;
else
a[i][j] = a[i][j - 1] + a[i + 1][j];
}
}
// Print the grid
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
// Driver code
int main()
{
int n = 5, m = 2;
// Function call
NumberOfShortestPaths(n, m);
return 0;
}
Java
// Java program to find number of shortest paths
class GFG
{
// Function to find number of shortest paths
static void NumberOfShortestPaths(int n, int m)
{
int [][]a = new int[n][m];
// Compute the grid starting from
// the bottom-left corner
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j < m; j++)
{
if (j == 0 || i == n - 1)
a[i][j] = 1;
else
a[i][j] = a[i][j - 1] + a[i + 1][j];
}
}
// Print the grid
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
// Driver code
public static void main(String[] args)
{
int n = 5, m = 2;
// Function call
NumberOfShortestPaths(n, m);
}
}
// This code is contributed by Princi Singh
Python3
# Python 3 program to find
# number of shortest paths
# Function to find number of shortest paths
def NumberOfShortestPaths(n, m):
a = [[0 for i in range(m)]
for j in range(n)]
for i in range(n):
for j in range(m):
a[i][j] = 0
# Compute the grid starting from
# the bottom-left corner
i = n - 1
while(i >= 0):
for j in range(m):
if (j == 0 or i == n - 1):
a[i][j] = 1
else:
a[i][j] = a[i][j - 1] + \
a[i + 1][j]
i -= 1
# Print the grid
for i in range(n):
for j in range(m):
print(a[i][j], end = " ")
print("\n", end = "")
# Driver code
if __name__ == '__main__':
n = 5
m = 2
# Function call
NumberOfShortestPaths(n, m)
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find number of shortest paths
using System;
class GFG
{
// Function to find number of shortest paths
static void NumberOfShortestPaths(int n, int m)
{
int [,]a = new int[n, m];
// Compute the grid starting from
// the bottom-left corner
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j < m; j++)
{
if (j == 0 || i == n - 1)
a[i, j] = 1;
else
a[i, j] = a[i, j - 1] + a[i + 1, j];
}
}
// Print the grid
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(a[i, j] + " ");
}
Console.Write("\n");
}
}
// Driver code
public static void Main(String[] args)
{
int n = 5, m = 2;
// Function call
NumberOfShortestPaths(n, m);
}
}
// This code is contributed by PrinciRaj1992
输出 :
1 5
1 4
1 3
1 2
1 1
时间复杂度: O(N×M)