Traverse Matrix in Diagonal strips

前端 未结 16 1796
暖寄归人
暖寄归人 2020-11-28 02:55

I thought this problem had a trivial solution, couple of for loops and some fancy counters, but apparently it is rather more complicated.

So my question is, how woul

相关标签:
16条回答
  • 2020-11-28 03:43

    The key is to iterate every item in the first row, and from it go down the diagonal. Then iterate every item in the last column (without the first, which we stepped through in the previous step) and then go down its diagonal.

    Here is source code that assumes the matrix is a square matrix (untested, translated from working python code):

    #define N 10
    void diag_step(int[][] matrix) {
        for (int i = 0; i < N; i++) {
            int j = 0;
            int k = i;
            printf("starting a strip\n");
            while (j < N && i >= 0) {
                printf("%d ", matrix[j][k]);
                k--;
                j++;
            }
            printf("\n");
        }
    
        for (int i = 1; i < N; i++) {
            int j = N-1;
            int k = i;
            printf("starting a strip\n");
            while (j >= 0 && k < N) {
                printf("%d ", matrix[k][j]);
                k++;
                j--;
            }
            printf("\n");
        }   
    }   
    
    0 讨论(0)
  • 2020-11-28 03:43

    you have to break the matrix in to upper and lower parts, and iterate each of them separately, one half row first, another column first. let us assume the matrix is n*n, stored in a vector, row first, zero base, loops are exclusive to last element.

    for i in 0:n
        for j in 0:i +1
            A[i + j*(n-2)]
    
    the other half can be done in a similar way, starting with:
    for j in 1:n
        for i in 0:n-j
            ... each step is i*(n-2) ...
    
    0 讨论(0)
  • 2020-11-28 03:44

    Let's take a look how matrix elements are indexed.

    (0,0)   (0,1)   (0,2)   (0,3)   (0,4)  
    (1,0)   (1,1)   (1,2)   (1,3)   (1,4)  
    (2,0)   (2,1)   (2,2)   (2,3)   (2,4)  
    

    Now, let's take a look at the stripes:

    Stripe 1: (0,0)
    Stripe 2: (0,1)    (1,0)  
    Stripe 3: (0,2)    (1,1)    (2,0)
    Stripe 4: (0,3)    (1,2)    (2,1)
    Stripe 5: (0,4)    (1,3)    (2,2)
    Stripe 6: (1,4)    (2,3)
    Stripe 7: (2,4)
    

    If you take a closer look, you'll notice one thing. The sum of indexes of each matrix element in each stripe is constant. So, here's the code that does this.

    public static void printSecondaryDiagonalOrder(int[][] matrix) {
        int rows = matrix.length;
        int cols = matrix[0].length;
        int maxSum = rows + cols - 2;
    
        for (int sum = 0; sum <= maxSum; sum++) {
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    if (i + j - sum == 0) {
                        System.out.print(matrix[i][j] + "\t");
                    }
                }
            }
            System.out.println();
        }
    }
    

    It's not the fastest algorithm out there (does(rows * cols * (rows+cols-2)) operations), but the logic behind it is quite simple.

    0 讨论(0)
  • 2020-11-28 03:44

    // This algorithm works for matrices of all sizes. ;)

        int x = 0;
        int y = 0;        
        int sub_x;
        int sub_y;
    
        while (true) {
    
            sub_x = x;
            sub_y = y;
    
            while (sub_x >= 0 && sub_y < y_axis.size()) {
    
                this.print(sub_x, sub_y);
                sub_x--;
                sub_y++;
    
            }
    
            if (x < x_axis.size() - 1) {
    
                x++;
    
            } else if (y < y_axis.size() - 1) {
    
                y++;
    
            } else {
    
                break;
    
            }
    
        }
    
    0 讨论(0)
  • 2020-11-28 03:46
    public void printMatrix(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        for (int i = 0; i < m + n - 1; i++) {
             int start_row = i < m ? i : m - 1;
             int start_col = i < m ? 0 : i - m + 1;
             while (start_row >= 0 && start_col < n) {
                   System.out.print(matrix[start_row--][start_col++]);
             }
             System.out.println("\n")
         }
    }
    
    0 讨论(0)
  • 2020-11-28 03:48
    static int[][] arr = {{ 1, 2, 3, 4},
                          { 5, 6, 7, 8},
                          { 9,10,11,12},
                          {13,14,15,16} };
    
    public static void main(String[] args) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < i+1; j++) {
                System.out.print(arr[j][i-j]);
                System.out.print(",");
            }
            System.out.println();
        }
    
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < arr.length-i; j++) {
                System.out.print(arr[i+j][arr.length-j-1]);
                System.out.print(",");
            }
            System.out.println();
        }
    }
    
    0 讨论(0)
提交回复
热议问题