Given a 2D array of characters with M rows and N columns. The task is to print all the possible paths from top (first row) to bottom (last row).
Examples:
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 cfiInput: arr[][] = {
{‘a’, ‘b’},
{‘d’, ‘e’}, }
Output:
ad ae
bd be
Approach:
- This problem can be solved using Depth First Traversal of an array with a slight modification.
- The modification here is to only iterate through the columns in the array until the target row is reached.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include<bits/stdc++.h> 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<<endl; } /* * 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 * */} // Driver code int main() { char inputchar[][2] = { { 'a', 'b' }, { 'd', 'e' } }; int R = sizeof(inputchar)/sizeof(inputchar[0]); int C = sizeof(inputchar[0])/sizeof(char); printPaths(inputchar, R, C); } // This code is contributed by chitranayal |
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
Time Complexity: O(R * C)
Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all possible paths in a DAG from vertex whose indegree is 0
- Print all paths from a given source to a destination
- Print all possible paths from top left to bottom right of a mXn matrix
- Print Palindromic Paths of Binary tree
- Print all palindromic paths from top left to bottom right in a matrix
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- C++ Program to print an Array using Recursion
- Print all possible combinations of r elements in a given array of size n
- Number of paths with exactly k coins
- Count all possible paths between two vertices
- Count even paths in Binary Tree
- Number of palindromic paths in a matrix
- Number of Paths of Weight W in a K-ary tree
- Count of palindromic plus paths in a given Matrix
- Number of Unicolored Paths between two nodes
- Unique paths in a Grid with Obstacles
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

