Code Golf:锯齿形图案扫描
挑战
按字符数计算的最短代码,采用单个输入整数 N
(N >= 3) 并返回一个索引数组,迭代时将遍历 N
x根据 JPEG“之字形”扫描模式的 N
矩阵。以下是遍历 8x8 矩阵的示例src:
< img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/JPEG_ZigZag.svg/220px-JPEG_ZigZag.svg.png" alt="zigzag 布局模式">
示例
(中间矩阵是不是输入或输出的一部分,只是输入所代表的 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 开始的)。
- 这与此问题相关。
额外奖励
扩展您的答案以获取两个输入 N
和 M
(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
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 matrixsrc:
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.
Bonus
Extend your answer to take two inputs N
and M
(N, M >=3) and perform the same scan over an N
xM
matrix. (In this case N
would be the number of columns and M
the number of rows.)
Bonus Examples
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
J,13
15个字符用法:
解释
(
NB.
是 J 的评论指示符)J,奖金,13 个字符
用法:
J, 13
15charactersUsage:
Explanation
(
NB.
is J's comment indicator)J, bonus, 13 characters
Usage:
Python, 92,
95,110,111,114,120, < s>122、162、164字符测试:
此解决方案可以轻松推广到
N
xM
板:调整输入处理并将N*N
替换为N*M
:我怀疑有一些更简单/更短的方法来读取两个数字。
测试:
Python, 92,
95,110,111,114,120,122,162,164charsTesting:
This solution easily generalizes for
N
xM
boards: tweak the input processing and replaceN*N
withN*M
:I suspect there's some easier/shorter way to read two numbers.
Testing:
Ruby, 69
89字符89 字符
运行Credits to doublep。
为排序方法
Ruby, 69
89chars89 chars
Run
Credits to doublep for the sort method.
F#,126 个字符
示例:
F#, 126 chars
Examples:
Golfscript,26/30
32/364559个字符迄今为止最短的非 J 解决方案:
更新排序(不要告诉其他人! ) - 30 个字符:
直接实现 - 36 个字符:
如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。
感谢 doublep 的排序技术。
说明:
对于密钥:
'* #(bonus)如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。
感谢 doublep 的排序技术。
说明:
对于密钥:
'* #solution 1 #~:\.*,{.\/1$\%+.1&@.~if]}直接实现 - 36 个字符:
如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。
感谢 doublep 的排序技术。
说明:
对于密钥:
'* #solution 2 #~\:1*,{..1/\1%+.2%.+(@*]}直接实现 - 36 个字符:
如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。
感谢 doublep 的排序技术。
说明:
对于密钥:
'* #(bonus) #~\:\*,{.\/1$\%+.1&@.~if]}直接实现 - 36 个字符:
如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。
感谢 doublep 的排序技术。
说明:
对于密钥:
'* #(bonus)直接实现 - 36 个字符:
如果您可以提供输出为“013642578”而不是“0 1 3 6 4 2 5 7 8”,那么您可以删除最后 4 个字符。
感谢 doublep 的排序技术。
说明:
对于密钥:
Golfscript, 26/30
32/364559charactersShortest non-J solution so far:
Updated sort (don't tell the others!) - 30 chars:
Straight implementation - 36 chars:
If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.
Credit to doublep for the sorting technique.
Explanation:
and for the key:
'* #(bonus)If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.
Credit to doublep for the sorting technique.
Explanation:
and for the key:
'* #solution 1 #~:\.*,{.\/1$\%+.1&@.~if]}Straight implementation - 36 chars:
If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.
Credit to doublep for the sorting technique.
Explanation:
and for the key:
'* #solution 2 #~\:1*,{..1/\1%+.2%.+(@*]}Straight implementation - 36 chars:
If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.
Credit to doublep for the sorting technique.
Explanation:
and for the key:
'* #(bonus) #~\:\*,{.\/1$\%+.1&@.~if]}Straight implementation - 36 chars:
If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.
Credit to doublep for the sorting technique.
Explanation:
and for the key:
'* #(bonus)Straight implementation - 36 chars:
If you can provide output as "013642578" instead of "0 1 3 6 4 2 5 7 8", then you can remove the last 4 characters.
Credit to doublep for the sorting technique.
Explanation:
and for the key:
MATLAB,101/116 个字符
它基本上是此处给出的相同答案的压缩版本,直接在命令提示符下运行:
以及从用户读取两个值的扩展:
测试:
和
MATLAB, 101/116 chars
Its basically a condensed version of the same answer given here, to be run directly on the command prompt:
and an extended one that read two values from the user:
Testing:
and
Ruby 137
130138个字符Ruby 137
130138charactersC89(280字节)
我想这仍然可以优化 - 我使用四个数组来存储可能的移动
撞墙时的矢量。我想这是可以做到的,在定义处节省一些字符,但我认为进一步实现逻辑会花费更多。不管怎样,就这样吧:
编译时有一些警告,但据我所知它是可移植的 C89。我其实不确定
不管我的算法是否聪明,也许你可以用更好的算法来缩短时间
(还没有花时间了解其他解决方案)。
C89 (280 bytes)
I guess this can still be optimized - I use four arrays to store the possible movement
vectors when hitting a wall. I guess it can be done, saving some chars at the definition, but I think it will cost more to implement the logic further down. Anyway, here you go:
compiles with a few warnings, but as far as I know it is portable C89. I'm actually not sure
whether my algorithm is clever at all, maybe you can get way shorter with a better one
(haven't taken the time to understand the other solutions yet).
Haskell 117 个字符
运行:
矩形变体稍长,为 120 个字符:
此处输入需要一个元组:
答案全部基于 0,并以列表形式返回(Haskell 的自然形式)。
Haskell 117 characters
Runs:
The rectangular variant is a little longer, at 120 characters:
Input here requires a tuple:
Answers are all 0-based, and returned as lists (natural forms for Haskell).
Perl 102 字符
用法:
Perl 102 characters
usage :