Zig Zag Matrix print multiple way


package com.java.zigzag;
/**
 * 
 * @author kmandal
 *
 */
public class PrintZigzagMatrix {
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("Right to left Zigzag convert");
zigzagRightToLeft(matrix, 0, 0, 0);
System.out.println();
System.out.println("Left to right Zigzag convert");
zigzagLeftToRight(matrix, 0, 0, 0);
System.out.println();
System.out.println("Left to right Zigzag convert");
zigOnlyLeftToRight(matrix, 0, 0, 0);
System.out.println();
System.out.println("Right to left Zigzag convert");
zigOnlyRightToLeft(matrix, 0, 0, 0);
System.out.println();
System.out.println("Zigzag using tail recursive");
zigzagTailRecursive(matrix, 0, 0, 0, 0, 0);
}
static void zigzagRightToLeft(int[][] matrix, int bottom, int top, int level) {
int m = matrix.length, n = matrix[0].length;
// traversing bottom to top
for (int i = bottom, j = top; i >= 0 && j < n; i--, j++) {
System.out.print(matrix[i][j] + " ");
}
// changing the index to traverse from bottom to top
if (bottom < m - 1) {
bottom++;
} else {
bottom = m - 1;
top++;
}
if (level++ < m + n - 1) {
zigzagLeftToRight(matrix, top, bottom, level++);
}
}
static void zigzagLeftToRight(int[][] matrix, int bottom, int top, int level) {
int m = matrix.length, n = matrix[0].length;
// traversing top to bottom
for (int i = bottom, j = top; i < m && j >= 0; i++, j--) {
System.out.print(matrix[i][j] + " ");
}
// changing the index to traverse from bottom to top
if (top < n - 1) {
top++;
} else {
bottom++;
top = n - 1;
}
if (level++ < m + n - 1) {
zigzagRightToLeft(matrix, top, bottom, level++);
}
}
static void zigOnlyLeftToRight(int[][] matrix, int bottom, int top,
int level) {
int m = matrix.length, n = matrix[0].length;
// traversing bottom to top
for (int j = bottom, k = top; j >= 0 && k < n; j--, k++) {
System.out.print(matrix[j][k] + " ");
}
// changing the index to traverse from bottom to top
if (bottom < m - 1) {
bottom++;
} else {
bottom = m - 1;
top++;
}
if (level++ < m + n - 1) {
zigOnlyLeftToRight(matrix, bottom, top, level++);
}
}
static void zigOnlyRightToLeft(int[][] matrix, int bottom, int top,
int level) {
int m = matrix.length, n = matrix[0].length;
// traversing top to bottom
for (int j = bottom, k = top; j < m && k >= 0; j++, k--) {
System.out.print(matrix[j][k] + " ");
}
// changing the index to traverse from bottom to top
if (top < n - 1) {
top++;
} else {
bottom++;
top = n - 1;
}
if (level++ < m + n - 1) {
zigOnlyRightToLeft(matrix, bottom, top, level++);
}
}
static void zigzagTailRecursive(int[][] matrix, int a, int b, int p,
int q, int level) {
int m = matrix.length, n = matrix[0].length;
if (level % 2 == 0 && level < m + n - 1) {
// traversing bottom to top
for (int j = a, k = b; j >= 0 && k < n; j--, k++) {
System.out.print(matrix[j][k] + " ");
}
} else if (level % 2 == 1 && level < m + n - 1) {
// traversing top to bottom
for (int j = p, k = q; j < m && k >= 0; j++, k--) {
System.out.print(matrix[j][k] + " ");
}
}
// changing the index to traverse from bottom to top
if (a < m - 1) {
a++;
} else {
a = m - 1;
b++;
}
// changing the index to traverse from bottom to top
if (q < n - 1) {
q++;
} else {
p++;
q = n - 1;
}
if (level++ < m + n - 1) {
zigzagTailRecursive(matrix, a, b, p, q, level++);
}
}
}
Out put:
Display the two dimensional matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Right to left Zigzag convert
1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 
Left to right Zigzag convert
1 5 2 3 6 9 13 10 7 4 8 11 14 15 12 16 
Left to right Zigzag convert
1 5 2 9 6 3 13 10 7 4 14 11 8 15 12 16 
Right to left Zigzag convert
1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16 
Zigzag using tail recursive
1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 


Previous
Next Post »