December 07, 2019

Unwinding a matrix in java

/**

Unwinding a matrix in java

 * Unwinding a matrix in java
 */
package com.konzern.solution;

/**
 * @author Gokul balan
 *
 */
public class Unwinding {

/**
* @param args
*/
public static void main(String[] args) {

Unwinding w = new Unwinding();
int[][] matrix = w.getInput();
w.display(matrix);
int[] value = w.unwindMatix(matrix);

}

private void display(int[][] matrix) {
int len = matrix.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}

}

private int[] unwindMatix(int[][] matrix) {
int len = matrix.length;
int loopCount = len * len;
int data[] = new int[loopCount];
int flag = 0;
int maxRow = len - 1;
int maxCol = len - 1;
int row = 0;
int col = 0;
for (int i = 0; i < loopCount; i++) {
data[i] = matrix[row][col];
System.out.print(data[i] + "\t");
switch (flag) {
case 0:
row = row;
col = col + 1;
if (col > maxCol) {
col = maxCol;
row = row + 1;
flag = 1;
}
break;
case 1:
row = row + 1;
col = col;
if (row > maxRow) {
row = maxRow;
flag = 2;
maxCol--;
}
break;
case 2:
row = row;
col = col - 1;
if (col < 0) {
flag = 3;
maxRow--;
}
break;

case 3:
row = row - 1;
col = col;
if (row < 0) {
flag = 0;
}
break;
default:
break;
}

}

return data;
}

private int[][] getInput() {
int matrix[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
return matrix;
}

}


Output

The marix

1 2 3
4 5 6
7 8 9

After Unwinding

1 2 3 6 9 8 7 4  5

Getting the exception


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at com.konzern.solution.Unwinding.unwindMatix(Unwinding.java:45)
at com.konzern.solution.Unwinding.main(Unwinding.java:20)


Source Code


/**
 * Unwinding a matrix in java
 */
package com.konzern.solution;

/**
 * @author Konzernites
 *
 */
public class Unwinding {

/**
* @param args
*/
public static void main(String[] args) {

Unwinding w = new Unwinding();
int[][] matrix = w.getInput();
w.display(matrix);
int[] value = w.unwindMatix(matrix);

}

private void display(int[][] matrix) {
int len = matrix.length;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}

}

private int[] unwindMatix(int[][] matrix) {
int len = matrix.length;
int loopCount = len * len;
int data[] = new int[loopCount];
int flag = 0;
int maxRow = len - 1;
int maxCol = len - 1;
int minRow = 0;
int minCol = 0;
int row = 0;
int col = 0;
for (int i = 0; i < loopCount; i++) {
data[i] = matrix[row][col];
System.out.print(data[i] + "\t");
switch (flag) {
case 0:
row = row;
col = col + 1;
if (col > maxCol) {
col = maxCol;
row = row + 1;
flag = 1;
minRow++;
}
break;
case 1:
row = row + 1;
col = col;
if (row > maxRow) {
row = maxRow;
col--;
flag = 2;
maxCol--;
}
break;
case 2:
row = row;
col = col - 1;
if (col < minCol) {
row = row - 1;
col = col + 1;
flag = 3;
maxRow--;
}
break;

case 3:
row = row - 1;
col = col;
if (row < minRow) {
row = row + 1;
col = col + 1;
flag = 0;
}
break;
default:
break;
}

}

return data;
}

private int[][] getInput() {
int matrix[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
return matrix;
}

}


Output

The marix

1 2 3
4 5 6
7 8 9

After Unwinding

1 2 3 6 9 8 7 4  5

Tik Toc Toe in python, two player game, playing with computer complete source code available soon...

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments