Traverse Matrix in Diagonal strips

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

    Pseudo code:

    N = 2 // or whatever the size of the [square] matrix
    for x = 0 to N
      strip = []
      y = 0
      repeat
         strip.add(Matrix(x,y))
         x -= 1
         y -= 1
      until x < 0
      // here to print the strip or do some' with it
    
    // And yes, Oops, I had missed it... 
    // the 2nd half of the matrix...
    for y = 1 to N    // Yes, start at 1 not 0, since main diagonal is done.
       strip = []
       x = N
       repeat
          strip.add(Matrix(x,y))
          x -= 1
          y += 1
       until x < 0
      // here to print the strip or do some' with it
    

    (Assumes x indexes rows, y indexes columns, reverse these two if matrix is indexed the other way around)

提交回复
热议问题