To print a zigzag traversal of a matrix using a purely
recursive approach, you can follow the logic of the zigzag traversal by
recursively processing each diagonal, either from top-to-bottom or
bottom-to-top. Below is a Java implementation that achieves this:
java
public class
ZigzagMatrixRecursive { public static void main(String[] args) { int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; System.out.println("Zigzag
traversal of the matrix:"); printZigzag(matrix, 0, 0, true); } // Recursive function to print the zigzag
traversal public static void printZigzag(int[][]
matrix, int row, int col, boolean isUp) { int m = matrix.length; int n = matrix[0].length; if (row >= m || col >= n) { return; } // Print current diagonal if (isUp) { printDiagonalUp(matrix, row,
col); } else { printDiagonalDown(matrix, row,
col); } // Determine next starting point for
the next diagonal if (col < n - 1) { printZigzag(matrix, row, col + 1,
!isUp); } else { printZigzag(matrix, row + 1, col,
!isUp); } } // Function to print a diagonal from
bottom-left to top-right private static void
printDiagonalUp(int[][] matrix, int row, int col) { int i = row; int j = col; while (i >= 0 && j <
matrix[0].length) { System.out.print(matrix[i][j] +
" "); i--; j++; } System.out.println(); } // Function to print a diagonal from
top-right to bottom-left private static void
printDiagonalDown(int[][] matrix, int row, int col) { int i = row; int j = col; while (i < matrix.length
&& j >= 0) { System.out.print(matrix[i][j] +
" "); i++; j--; } System.out.println(); } } |
How It Works
- printZigzag
Method:
- This
is the main recursive method that manages the zigzag traversal. It takes
the matrix, the current starting row (row) and column (col), and a
boolean isUp that indicates the direction of the diagonal traversal.
- The
recursion continues until the row or column exceeds the matrix bounds.
- Depending
on the value of isUp, it either prints a diagonal in an upward direction
(printDiagonalUp) or a downward direction (printDiagonalDown).
- After
printing a diagonal, it determines the next diagonal's starting point:
- If
the current column is not the last, the next diagonal starts at the same
row and the next column (col + 1).
- If
the current column is the last, the next diagonal starts at the next row
(row + 1) and the same column.
- printDiagonalUp
Method:
- This method prints a diagonal line in the upward direction, starting from the provided row and col and moving up-left to down-right.
- printDiagonalDown
Method:
- This method prints a diagonal line in the downward direction, starting from the provided row and col and moving from down-left to top-right.
Example Output
For the input matrix:
1 2
3 4 5 6
7 8 9 10 11 12 13 14 15 16 |
The output will be:
1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 |
Summary
This approach allows you to print a matrix in a zigzag
pattern recursively, alternating the direction of each diagonal. The recursion
effectively handles the traversal of diagonals, making the code both concise
and elegant.
0 Comments