问题描述
我有一个保证是一个完美的方阵。我想开始在矩阵在这种情况下,这将是中心矩阵[2] [2]
,我知道如何计算中心 (INT)(尺寸/ 2)
。我需要输出数组的内容在这下面的向外螺旋图案。当然,算法应该与任何完美的方阵。我不知道,如果该算法已经存在,我不想重新发明轮子。
INT尺寸/ 2;
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
这个例子的输出应该是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
由于这闻起来像功课那么没有code或直接回答,而不只是一些提示:
你可以看一下这是一个乌龟的问题:
让 M
是由 1
细胞和研究
是由 90 度的螺旋方向(顺时针或逆时针)。然后螺旋可以是连接$ C $光盘作为系列形成该图案(从起点)龟命令:
M,R,M,R,
M,M,R,M,M,R,
M,M,M,R,M,M,M,R,
M,M,M,M,R,M,M,M,M,R,
M,M,M,M,M,R
正如你可以看到你开始用1X移动再经过两个重复旋转,切换到2倍的举动,经过2移动切换到3倍的举动,...等等。这可以通过仅几步来完成的循环(或者只是一个有一些适当的迭代和停止时,细胞的基质数量被击中......或终端角命中)
您需要处理奇/偶矩阵大小
对于奇数矩阵规模的中间点容易。因为即使大小是一个有点复杂。如果使用正转,然后使用尺寸减半的舍去除法结果,并开始与移动到右侧。 (如果你需要不同的螺旋,那么你需要添加 +1
到 X
和/或是
键,开始改变方向)螺旋将保持居中。
所以如果得了甚至尺寸矩阵那么最后的运动是两次,如果你有奇数大小则最后的运动是有(在这个例子中一样)只有一次
轮换
有方向的二维矢量存储。例如: D =(+ 1,0)
手段的权利。旋转2D矢量你只是换坐标,否定一个轴(其中之一指的CW / CCW)。例如(X,Y) - > (Y,-x)
移动
存储当前位置为二维矢量了。该运动只是增加电流的方向向量来了。
有乐趣解决这个...
I have an guaranteed to be a perfect square matrix. I want to start at the center of the matrix in this case it would be matrix[2][2]
, I know how to figure the center (int)(dimensions / 2)
. I need to output the contents of the array in this following outward spiral pattern. Of course the algorithm should work with any perfect square matrix. I wasn't sure if this algorithm already existed and I didn't want to re-invent the wheel.
int dimensions / 2;
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
The output for this example should be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
As this smells like homework then no code or direct answer instead just few hints:
You can look at this as an turtle problem:
Let m
be movement by 1
cell and r
be rotation by 90
degrees in your spiral direction (CW or CCW). then the spiral can be encoded as series of turtle commands forming this pattern (from the start point):
m,r,m,r,
m,m,r,m,m,r,
m,m,m,r,m,m,m,r,
m,m,m,m,r,m,m,m,m,r,
m,m,m,m,m,r
As you can see you start with 1x move then rotate after two repetition you switch to 2x move, after 2 moves switch to 3x move,... and so on. This can be done with just few for loops (or just by one with some proper iterations and stopping when matrix number of cells is hit ... or the endpoint corner is hit)
You need to handle even/odd matrix sizes
for odd matrix sizes is the middle point easy. For even sizes it is a bit more complicated. if you use CW rotation then use the rounded down division result of halving the size and start with moving to the right. (if you need different spiral then you need to add +1
to x
and/or y
and change starting direction) so the spiral will stay centered.
So If you got even sized matrix then the last movement is twice if you got odd size then the last movement is there just once (like in this example)
rotation
Have direction stored as 2D vector. for example d=(+1,0)
means right. to rotate 2D vector you just swap coordinates and negate one axis (which one means the CW/CCW). For example (x,y) -> (y,-x)
movement
Store current position as 2D vector too. The movement is just adding current direction vector to it.
Have fun with solving this ...
这篇关于顺时针打印2-D阵列从中心扩展的螺旋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!