Header Ads Widget

Responsive Advertisement

Zig Zag Matrix print multiple way

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

  1. printZigzag Method:
    1. 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.
    2. The recursion continues until the row or column exceeds the matrix bounds.
    3. Depending on the value of isUp, it either prints a diagonal in an upward direction (printDiagonalUp) or a downward direction (printDiagonalDown).
    4. After printing a diagonal, it determines the next diagonal's starting point:
      1. If the current column is not the last, the next diagonal starts at the same row and the next column (col + 1).
      2. If the current column is the last, the next diagonal starts at the next row (row + 1) and the same column.
  2. printDiagonalUp Method:
    1. This method prints a diagonal line in the upward direction, starting from the provided row and col and moving up-left to down-right.
  3. printDiagonalDown Method:
    1. 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.

 





Zig Zag Matrix print multiple way
Zig Zag Matrix print multiple way




Post a Comment

0 Comments