给定两个数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
方法:一种有效的方法是从左下角开始计算网格。
- 到达 cell(n, i) 的最短路径数为 1,其中 1 < = i < = M
- 到达 cell(i, 1) 的最短路径数为 1,其中 1 < = i < = N
- 到达cell(i, j)的最短路径数是cell(i-1, j)和(i, j+1)的最短路径数之和,其中,1 < = j < = M and 1 < = i < = 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。