给定具有M行N列的2D字符数组。任务是打印从顶部(第一行)到底部(最后一行)的所有可能路径。
例子:
Input: arr[][] = {
{‘a’, ‘b’, ‘c’},
{‘d’, ‘e’, ‘f’},
{‘g’, ‘h’, ‘i’}}
Output:
adg adh adi aeg aeh aei afg afh afi
bdg bdh bdi beg beh bei bfg bfh bfi
cdg cdh cdi ceg ceh cei cfg cfh cfi
Input: arr[][] = {
{‘a’, ‘b’},
{‘d’, ‘e’}, }
Output:
ad ae
bd be
方法:
- 可以通过对数组进行深度优先遍历并稍加修改来解决此问题。
- 此处的修改是仅迭代数组中的列,直到到达目标行。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
void dfs(char inputchar[][2], string res,
int i, int j, int R, int C)
{
// If the current row index equals to R, it
// indicates we have reached the bottom of
// the array and hence we print the result
if (i == R)
{
cout << res << " ";
return;
}
res = res + (inputchar[i][j]);
// Iterate over each of the columns
// in the array
for (int k = 0; k < C ; k++)
{
dfs(inputchar, res, i + 1, k, R, C);
if (i + 1 == R)
break;
}
}
// Function to print all the possible paths
void printPaths(char inputchar[][2], int R, int C)
{
for (int i = 0; i < C; i++)
{
dfs(inputchar, "", 0, i, R, C);
cout<
Java
// Java implementation of the approach
public class GFG {
// Function to print all the possible paths
private static void printPaths(char[][] input,
int R, int C)
{
for (int i = 0; i < C; i++) {
dfs(input, "", 0, i, R, C);
System.out.println();
}
}
/**
* Depth first traversal of the array
*
* @param input array of characters
* @param res to be printed in console
* @param i current row index
* @param j current column index
* @param R number of rows in the input array
* @param C number of rows in the output array
*/
private static void dfs(char[][] input, String res,
int i, int j, int R, int C)
{
// If the current row index equals to R, it
// indicates we have reached the bottom of
// the array and hence we print the result
if (i == R) {
System.out.print(res + " ");
return;
}
res = res + input[i][j];
// Iterate over each of the columns
// in the array
for (int k = 0; k < C; k++) {
dfs(input, res, i + 1, k, R, C);
if (i + 1 == R) {
break;
}
}
}
// Driver code
public static void main(String[] args)
{
char[][] input = {
{ 'a', 'b' },
{ 'd', 'e' }
};
int R = input.length;
int C = input[0].length;
printPaths(input, R, C);
}
}
Python3
# Python3 implementation of the approach
# Function to print all the possible paths
def printPaths(inputchar, R, C) :
for i in range(C) :
dfs(inputchar, "", 0, i, R, C);
print()
"""
* Depth first traversal of the array
*
* @param input array of characters
* @param res to be printed in console
* @param i current row index
* @param j current column index
* @param R number of rows in the input array
* @param C number of rows in the output array
* """
def dfs(inputchar, res, i, j, R, C) :
# If the current row index equals to R, it
# indicates we have reached the bottom of
# the array and hence we print the result
if (i == R) :
print(res, end = " ");
return;
res = res + inputchar[i][j];
# Iterate over each of the columns
# in the array
for k in range(C) :
dfs(inputchar, res, i + 1, k, R, C);
if (i + 1 == R) :
break;
# Driver code
if __name__ == "__main__" :
inputchar = [
[ 'a', 'b' ],
[ 'd', 'e' ]
];
R = len(inputchar);
C = len(inputchar[0]);
printPaths(inputchar, R, C);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print all the possible paths
private static void printPaths(char[,] input,
int R, int C)
{
for (int i = 0; i < C; i++)
{
dfs(input, "", 0, i, R, C);
Console.WriteLine();
}
}
/**
* Depth first traversal of the array
*
* @param input array of characters
* @param res to be printed in console
* @param i current row index
* @param j current column index
* @param R number of rows in the input array
* @param C number of rows in the output array
*/
private static void dfs(char[,] input, string res,
int i, int j, int R, int C)
{
// If the current row index equals to R, it
// indicates we have reached the bottom of
// the array and hence we print the result
if (i == R)
{
Console.Write(res + " ");
return;
}
res = res + input[i,j];
// Iterate over each of the columns
// in the array
for (int k = 0; k < C; k++)
{
dfs(input, res, i + 1, k, R, C);
if (i + 1 == R)
{
break;
}
}
}
// Driver code
public static void Main()
{
char[,] input = {
{ 'a', 'b' },
{ 'd', 'e' }
};
int R = input.GetLength(0);
int C = input.GetLength(1);
printPaths(input, R, C);
}
}
// This code is contributed by AnkitRai01
输出:
ad ae
bd be
时间复杂度: O(R * C)
空间复杂度: O(1)