如何在C中旋转1D数组的一部分? [英] How to rotate a section of a 1d array in C?

查看:91
本文介绍了如何在C中旋转1D数组的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打印了一个6x6 1d数组,但想逆时针旋转左上3x3部分。是否有一种算法?将来,我也想像右下3x3部分或右上3x3部分或左下3x3部分一样旋转。

I printed out a 6x6 1d array but want to rotate counter-clockwise the top left 3x3 section. Is there an algorithm for this? In the future I also want to rotate like the bottom right 3x3 section or the top right 3x3 section or the bottom left 3x3 section.

a[0]  a[1]  a[2]  a[3]  a[4]  a[5]         a[1]  a[2]  a[8]  a[3]  a[4]  a[5]
a[6]  a[7]  a[8]  a[9]  a[10] a[11]        a[0]  a[7]  a[14] a[9]  a[10] a[11]
a[12] a[13] a[14] a[15] a[16] a[17]  --->  a[6]  a[12] a[13] a[18] a[19] a[20]
a[18] a[19] a[20] a[21] a[22] a[23]        a[18] a[19] a[20] a[21] a[22] a[23]
a[24] a[25] a[26] a[27] a[28] a[29]        a[24] a[25] a[26] a[27] a[28] a[29]
a[30] a[31] a[32] a[33] a[34] a[35]        a[30] a[31] a[32] a[33] a[34] a[35]


推荐答案

在此处,区分事物在内存中的分配方式和表示方式非常重要。没有 6x6 1D数组之类的东西,因为1D数组没有行和列。因此,首先将该1D数组转换为2x 6D6数组。

Here it is important to distinguish between how things are allocated in memory and how you represent them. There is no such thing as a "6x6 1D array", because 1D arrays don't have rows and columns. So start by converting this 1D array to a 2D array 6x6.

然后可以通过指定坐标(x,y)定义旋转的中心。您应该理智地检查这些坐标,以使它们不在矩阵的边缘(或者,如果需要,可以设计算法使之可行)。

You can then define a center for the rotation by specifying coordinates (x, y). You should sanity check these coordinates so that they are not at the edges of the matrix (alternatively, design the algorithm so that this is possible, if needed).

最直截了当的解决方案是只获取围绕中心的索引并以硬编码方式移动数据:

The blunt solution is to just grab the indices around the center and move data around in a hard-coded manner:

array[center_x-1][center_y-1] = array[center_x][center_y-1];
...

,依此类推。这将是最快的方法,而最简单的解决方案通常是最好的解决方案。

and so on. This will be the fastest way and the simplest solution is often the best one.

更具模块化的方法可以实现可变旋转方向将是创建一个指针数组,该指针数组指向需要旋转的围绕中心的数据。指针数组可以实现为链表的一种简单形式:

A more modular approach which allows for a variable rotating direction would be to create an array of pointers pointing at the data around the center that needs to be rotated. This array of pointers can be implemented as a simple form of linked list:

typedef struct rotate_node_t rotate_node_t;
typedef struct rotate_node_t
{
  rotate_node_t* next;
  rotate_node_t* prev;
  int* data;
} rotate_node_t;

您将有 rotate_node_t轮播[8] 可能将其索引分配为:

You'd have a rotate_node_t rotation [8] which could have its indices assigned as:

0 1 2
7 c 3
6 5 4

其中 c为中心。

有了这个,您可以简单地在任何方向上遍历链表并将数据从一个节点移到另一个节点。它比直接数组访问更灵活,但速度更慢,更复杂。并且可以扩展以支持各种形式的狂野轮换模式。

With this in place, you can simply iterate through the linked list in any direction and move data from one node to the other. It is more flexible than direct array access, but slower and more complex. And it could be expanded to support all manner of wild rotation patterns.

这篇关于如何在C中旋转1D数组的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆