Traverse Matrix in Diagonal strips

前端 未结 16 1841
暖寄归人
暖寄归人 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

    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) ...
    

提交回复
热议问题