Code Golf: Zigzag pattern scanning
Posted
by fbrereto
on Stack Overflow
See other posts from Stack Overflow
or by fbrereto
Published on 2010-06-11T19:20:00Z
Indexed on
2010/06/11
19:22 UTC
Read the original article
Hit count: 421
The Challenge
The shortest code by character count that takes a single input integer N
(N >= 3) and returns an array of indices that when iterated would traverse an N
xN
matrix according to the JPEG "zigzag" scan pattern. The following is an example traversal over an 8x8 matrix (referenced from here:)
Examples
(The middle matrix is not part of the input or output, just a representation of the NxN matrix the input represents.)
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
Notes:
- The resulting array's base should be appropriate for your language (e.g., Matlab arrays are 1-based, C++ arrays are 0-based).
- This is related to this question.
© Stack Overflow or respective owner