矩阵和算法“螺旋”
我想问是否有一些算法准备好,允许我这样做:我有一个矩阵 m (col) xn (row) 与 mxn 元素。我想给这个元素从中心开始并以螺旋形式旋转的位置,例如,对于 3x3 矩阵,我有 9 个元素,这样定义:
5 6 7
4 9 8
3 2 1
或者对于 una 矩阵 4 x 3,我有 12 个元素,定义:
8 9 10 1
7 12 11 2
6 5 4 3
或者再次,一个矩阵5x2 我有 10 个这样定义的元素:
3 4
7 8
10 9
6 5
2 1
等等。 我已经解决了基本上定义 mxn 元素的整数数组并手动加载值的问题,但对我来说,就像自动由算法生成的矩阵一样。 感谢谁能帮我找到一些东西,非常感谢。
更新
这段代码,完全符合我想要的,但不是在delphi中;只是我需要从 1 开始,而不是从 0 开始。对我来说重要的是它对任何矩阵 mx n 都有效。谁帮我翻译一下delphi?
(defun spiral (rows columns)
(do ((N (* rows columns))
(spiral (make-array (list rows columns) :initial-element nil))
(dx 1) (dy 0) (x 0) (y 0)
(i 0 (1+ i)))
((= i N) spiral)
(setf (aref spiral y x) i)
(let ((nx (+ x dx)) (ny (+ y dy)))
(cond
((and (< -1 nx columns)
(< -1 ny rows)
(null (aref spiral ny nx)))
(setf x nx
y ny))
(t (psetf dx (- dy)
dy dx)
(setf x (+ x dx)
y (+ y dy)))))))
> (pprint (spiral 6 6))
#2A ((0 1 2 3 4 5)
(19 20 21 22 23 6)
(18 31 32 33 24 7)
(17 30 35 34 25 8)
(16 29 28 27 26 9)
(15 14 13 12 11 10))
> (pprint (spiral 5 3))
#2A ((0 1 2)
(11 12 3)
(10 13 4)
(9 14 5)
(8 7 6))
再次非常感谢。
i wanted ask if there some algorithm ready, that allowed me to do this: i have a matrix m (col) x n (row) with m x n elements. I want give position to this element starting from center and rotating as a spiral, for example, for a matrix 3x3 i have 9 elements so defined:
5 6 7
4 9 8
3 2 1
or for una matrix 4 x 3 i have 12 elements, do defined:
8 9 10 1
7 12 11 2
6 5 4 3
or again, a matrix 5x2 i have 10 elements so defined:
3 4
7 8
10 9
6 5
2 1
etc.
I have solved basically defining a array of integer of m x n elements and loading manually the value, but in generel to me like that matrix maked from algorithm automatically.
Thanks to who can help me to find something so, thanks very much.
UPDATE
This code, do exactely about i want have, but not is in delphi; just only i need that start from 1 and not from 0. Important for me is that it is valid for any matrics m x n. Who help me to translate it in delphi?
(defun spiral (rows columns)
(do ((N (* rows columns))
(spiral (make-array (list rows columns) :initial-element nil))
(dx 1) (dy 0) (x 0) (y 0)
(i 0 (1+ i)))
((= i N) spiral)
(setf (aref spiral y x) i)
(let ((nx (+ x dx)) (ny (+ y dy)))
(cond
((and (< -1 nx columns)
(< -1 ny rows)
(null (aref spiral ny nx)))
(setf x nx
y ny))
(t (psetf dx (- dy)
dy dx)
(setf x (+ x dx)
y (+ y dy)))))))
> (pprint (spiral 6 6))
#2A ((0 1 2 3 4 5)
(19 20 21 22 23 6)
(18 31 32 33 24 7)
(17 30 35 34 25 8)
(16 29 28 27 26 9)
(15 14 13 12 11 10))
> (pprint (spiral 5 3))
#2A ((0 1 2)
(11 12 3)
(10 13 4)
(9 14 5)
(8 7 6))
Thanks again very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基于经典的螺旋算法。支持非方阵:
3 x 3:
4 x 3:
2 x 5:
Based on the classic spiral algorithm. supporting non-square matrix:
3 x 3:
4 x 3:
2 x 5:
给你吧!在出现 30 个语法错误之后...
在 ideone.com 上,我对其进行了一些测试,似乎运行良好。我想你仍然可以看到输出并自己运行它......
我在代码中添加了一些注释。足以理解大部分内容。主导航系统有点难以解释。简而言之,做螺旋就是沿第一个方向 1 次,第二个方向 1 次,第三个方向 2 次,第四个方向 2 次,第五个方向 3 次,3、4、4、5、5 次,依此类推。我使用所谓的“种子”和“步骤”来获得此行为。
如果您确实需要它来打印文本螺旋,我会让您对齐数字。只需用空格填充它们即可。
编辑:
忘记了......为了使其在 ideone 上工作,我将参数放在 2 行上作为输入。 m,然后n。
例如:
产量
There you go!!! After 30some syntax errors...
On ideone.com, I ran it with some tests and it seems to work fine. I think you can see the output there still and run it yourself...
I put some comments in the code. Enough to understand most of it. The main navigation system is a little bit harder to explain. Briefly, doing a spiral is going in first direction 1 time, second 1 time, third 2 times, fourth 2 times, fifth 3 times, 3, 4, 4, 5, 5, and so on. I use what I called a
seed
andstep
to get this behavior.If you really need that to print text spirals I'll let you align the numbers. Just pad them with spaces.
EDIT:
Was forgetting... In order to make it work on ideone, I put the parameters on 2 lines as input. m, then n.
For example:
yields
以下是您想要完成的任务的带注释的 JavaScript 实现。
该代码不是最有效的,因为它天真地沿着螺旋走,而没有首先检查它所走的位置是否有效。它仅在尝试设置值之前检查当前位置的有效性。
Here's the commented JavaScript implementation for what you're trying to accomplish.
The code isn't the most efficient, because it walks the spiral naively without first checking if the location it's walking on is valid. It only checks the validity of the current location right before it tries to set the value on it.
尽管问题已经得到解答,但这是一种替代解决方案(可以说更简单)。
解决方案是在 python 中(使用 numpy 作为双向数组),但可以轻松移植。
基本思想是利用已知步数 (m*n) 作为结束条件,
并在每次迭代时正确计算循环的下一个元素:
这里有一些输出:
Even though the question is already answered, this is an alternative solution (arguably simpler).
The solution is in python (using numpy for bidimendional arrays), but can be easily ported.
The basic idea is to use the fact that the number of steps is known (m*n) as end condition,
and to properly compute the next element of the loop at each iteration:
And here some outputs: