Locked. This question and its answers are locked,因为该问题是题外话,但具有历史意义。它目前不接受新的答案或互动。








挑战

按字符计数的最短代码,它采用单个输入整数N(N> = 3),并返回一个索引数组,该索引数组在迭代时将根据JPEG“zigzag”扫描模式遍历N x N矩阵。以下是遍历8x8矩阵src的示例:

例子

(中间矩阵不是输入或输出的一部分,只是输入代表的NxN矩阵的表示。)
                1 2 3
(Input) 3  -->  4 5 6  -->  1 2 4 7 5 3 6 8 9 (Output)
                7 8 9

                1  2  3  4
(Input) 4  -->  5  6  7  8  -->  1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16 (Output)
                9 10 11 12
               13 14 15 16

笔记
  • 结果数组的基数应适合您的语言(例如,Matlab数组基于1,C++数组基于0)。
  • 这与this question有关。

  • 奖金

    扩展答案以获取两个输入NM(N,M> = 3),并在N x M矩阵上执行相同的扫描。 (在这种情况下,N将是列数,M将是行数。)

    奖金示例
                      1  2  3  4
    (Input) 4 3  -->  5  6  7  8  -->  1 2 5 9 6 3 4 7 10 11 8 12 (Output)
                      9 10 11 12
    
                       1  2  3
    (Input) 3 4  -->   4  5  6  -->  1 2 4 7 5 3 6 8 10 11 9 12 (Output)
                       7  8  9
                      10 11 12
    

    最佳答案

    J,13个15个字符

    ;<@|.`</.i.2$
    

    用法:
       ;<@|.`</.i.2$  3
    0 1 3 6 4 2 5 7 8
    
       ;<@|.`</.i.2$  4
    0 1 4 8 5 2 3 6 9 12 13 10 7 11 14 15
    

    说明

    (NB.是J的注释指示符)
    ;         NB. Link together...
    <@|.`<    NB. ... 'take the reverse of' and 'take normally'
    /.        NB. ... applied to alternating diagonals of...
    i.        NB. ... successive integers starting at 0 and counting up to fill an array with dimensions of...
    2$        NB. ... the input extended cyclically to a list of length two.
    

    J,奖金,13个字符
    ;<@|.`</.i.|.
    

    用法:
       ;<@|.`</.i.|. 3 4
    0 1 3 6 4 2 5 7 9 10 8 11
    
       ;<@|.`</.i.|. 9 6
    0 1 9 18 10 2 3 11 19 27 36 28 20 12 4 5 13 21 29 37 45 46 38 30 22 14 6 7 15 23 31 39 47 48 40 32 24 16 8 17 25 33 41 49 50 42 34 26 35 43 51 52 44 53
    

    10-06 05:06
    查看更多