How can I read a zig zag matrix starting from the secondary diagonal?

0

I need to read them diagonally. so:

I have managed to get the matrix to start from the reverse diagonal, but always from top to bottom .... I need it to go up and down as seen in the image.

This is what I tried so far:

System.out.println("");
for(int i=0; i<mat.length; i++){
    for(int j=mat.length-1, l=i; l<mat.length; j--, l++){
        System.out.print(mat[l][j]+" ");
    }
    System.out.println();
}
    
asked by Ronin 17.02.2017 в 22:32
source

2 answers

1
public static void main(String[] args) {
    int[][] arreglo = { {1,2,25}, {8,24,26}, {23,27,28} };
    int x = arreglo.length-1;
    int y = 0;
    int i = 0;
    boolean abajo = true;
    int[] resultado = new int[(arreglo.length*(arreglo.length+1))/2];
    do{
        resultado[i++] = arreglo[x][y];
        System.out.println(String.format("(x:%d,y:%d)=%d", x,y,arreglo[x][y]));
        if (y==(arreglo[0].length-1) && abajo){
            x++; abajo=false;
        } else if (x==(arreglo.length-1) && !abajo){
            y++; abajo=true;
        } else if (abajo){
            x--; y++;
        } else {
            x++; y--;
        }
    }while (x<arreglo.length);

}

That gives you the corrido indicated

    
answered by 18.02.2017 в 00:31
0

If the matrix is of size n and we call it A.

for(int k = 0; k < n; k++) {
    int j = 0;
    if(k % 2 == 0)
        for(int i = k; i < n; i++) {
            System.out.println(A[i][n - 1 - j]);
            j++;
        }
    else
        for(int i = k; i < n; i++) {
            System.out.println(A[n - 1 - j][i]);
            j++;
        }
}
    
answered by 18.02.2017 в 00:56