Dada una array arr de tamaño N x M , la tarea es atravesar esta array usando recursividad .
Ejemplos:
Input: arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output: 1, 2, 3, 4, 5, 6, 7, 8, 9 Input: M[][] = {{11, 12, 13}, {14, 15, 16}, {17, 18, 19}} Output: 11, 12, 13, 14, 15, 16, 17, 18, 19
Enfoque: A continuación se muestran los pasos para atravesar la Matrix recursivamente:
- Caso base: si la fila o columna actual excede el tamaño N o M, respectivamente, la recursividad se detiene.
- Si la columna actual excede el número total de columnas M, entonces se inicia la siguiente fila.
if (current_col >= M) This row ends. Start for next row
- Si la fila actual excede el número total de filas N, se detiene el recorrido completo.
if (current_row >= N) Matrix has been traversed completely
- Caso Recursivo: En cada llamada recursiva,
- Se imprime el elemento actual de Matrix.
- Se realizan dos llamadas recursivas:
- Uno para atravesar la siguiente fila, y
- Otro para atravesar la siguiente columna.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to traverse the matrix recursively #include <iostream> using namespace std; #define N 2 #define M 3 // Function to traverse the matrix recursively int traverseMatrix(int arr[N][M], int current_row, int current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix cout << arr[current_row][current_col] << ", "; // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code int main() { int arr[N][M] = { { 1, 2, 3 }, { 4, 5, 6 } }; traverseMatrix(arr, 0, 0); return 0; }
Java
// Java program to traverse the matrix recursively class GFG { final static int N = 2; final static int M = 3; // Function to traverse the matrix recursively static int traverseMatrix(int arr[][], int current_row, int current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix System.out.print(arr[current_row][current_col] + ", "); // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code public static void main (String[] args) { int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; traverseMatrix(arr, 0, 0); } } // This code is contributed by AnkitRai01
C#
// C# program to traverse the matrix recursively using System; public class GFG { static int N = 2; static int M = 3; // Function to traverse the matrix recursively static int traverseMatrix(int [,]arr, int current_row, int current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix Console.Write(arr[current_row,current_col] + ", "); // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code public static void Main (string[] args) { int [,]arr = { { 1, 2, 3 }, { 4, 5, 6 } }; traverseMatrix(arr, 0, 0); } } // This code is contributed by AnkitRai01
Python3
# Python3 program to traverse the matrix recursively N = 2 M = 3 # Function to traverse the matrix recursively def traverseMatrix(arr, current_row, current_col) : # If the entire column is traversed if (current_col >= M) : return 0; # If the entire row is traversed if (current_row >= N) : return 1; # Print the value of the current # cell of the matrix print(arr[current_row][current_col],end= ", "); # Recursive call to traverse the matrix # in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1 ) == 1) : return 1; # Recursive call for changing the # Row of the matrix return traverseMatrix(arr, current_row + 1, 0); # Driver code if __name__ == "__main__" : arr = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; traverseMatrix(arr, 0, 0); # This code is contributed by AnkitRai01
Javascript
<script> // Javascript program to traverse the matrix recursively let N = 2 let M = 3 // Function to traverse the matrix recursively function traverseMatrix(arr, current_row, current_col) { // If the entire column is traversed if (current_col >= M) return 0; // If the entire row is traversed if (current_row >= N) return 1; // Print the value of the current // cell of the matrix document.write(arr[current_row][current_col] + ", "); // Recursive call to traverse the matrix // in the Horizontal direction if (traverseMatrix(arr, current_row, current_col + 1) == 1) return 1; // Recursive call for changing the // Row of the matrix return traverseMatrix(arr, current_row + 1, 0); } // Driver code let arr = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; traverseMatrix(arr, 0, 0); </script>
Producción:
1, 2, 3, 4, 5, 6,
Complejidad de tiempo: O(N * M)
Espacio auxiliar: O(M), debido a llamadas recursivas
Publicación traducida automáticamente
Artículo escrito por MohammadMudassir y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA