如何生成循环矩阵?
我想用 C 或 C++ 生成一个循环矩阵。
对于 n = 3,如何生成下面的矩阵?
1 2 3
8 9 4
7 6 5
I want to generate a circular matrix in C or C++.
How can I generate the matrix below, for n = 3?
1 2 3
8 9 4
7 6 5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我以前做过几次......
伪代码:
I did it some times ago...
Pseudocode:
作为 @Rin 答案的替代方案,您可以考虑按线性顺序存储矩阵,然后在访问它时重新映射索引。如果您使用 C++,则可以通过访问器函数封装此重新映射,例如:
As an alternative to @Rin's answer, you could consider storing the matrix in linear order, and then re-mapping indices when accessing it. If you're in C++, you can encapsulate this re-mapping via the accessor functions, e.g.:
这个问题是在微软笔试中问到的。
因此考虑给出完整的代码。
下面的代码适用于运行时给定的任意数量的行和任意数量的列。
无需对尺寸进行硬编码。
This question asked in Microsoft written test.
Hence considering to give full code.
Below code works for any number of rows and any number of columns given at runtime.
No need of hardcoding the dimensions.
首先将你的矩阵清空。在我的示例中,我在 std::pair 上使用 std::map,但您也可以使用二维数组。我使用 std::map 因为它更容易看到元素何时丢失。
然后创建一个包含您想要移动的不同方向的集合。
如果先向右移动,则意味着X加1,Y不变。然后我们向下移动,意味着 Y 增加 1 并离开 X。
初始化你的起始坐标,它们移动直到到达“边界”。边界可以是边框,也可以是已填充的单元格。
然后在一个循环中(但我将其作为练习,供您写出来:-))只需填充矩阵:
并移动到下一个位置;
检查 nextPosition 以查看您是否在矩阵之外(nextPosition.first/second < 0 或 >= 矩阵大小)。
如果您仍在矩阵内,请在地图中使用 std::find 来查看该条目是否已被填充:
如果您撞到矩阵的边界或撞到已填充的条目,请采取再次当前位置,增加 currentMovePosition 以更改方向并重试。
如果改变方向,请务必环绕 currentMovePosition。
继续这样做,直到矩阵完全填满。
要判断矩阵是否完全填充,可以检查所有4个方向是否都移动到已经填充的元素,但更简单的方法是简单地计算填充单元格的数量,如果等于矩阵的大小*大小则停止。
First make your matrix empty. In my example, I use an std::map on an std::pair, but you could also use a 2-dimensional array. I use std::map because it's easier to see when an element is missing.
Then make a collection that contains the different directions in which you want to move.
If first want to move to the right, it means incrementing X by 1, and leaving Y as it is. Then we move down, meaning incrementing Y by 1 and leaving X.
Initialize your starting coordinate and them move until you reach a 'boundary'. A boundary is either the border or a cell that has already been filled in.
Then in a loop (but I leave this as an excercise for you to write this out :-)) simply fill in the matrix:
and move to the next position;
check the nextPosition to see if you are outside your matrix (nextPosition.first/second < 0 or >= size of matrix).
If you are still within the matrix use std::find in the map to see if this entry has already been filled in:
If you bump against the boundaries of the matrix or you bump into an entry that has already been filled in, take the current position again, increment currentMovePosition to change the direction and try again.
Be sure to wrap currentMovePosition around if you change direction.
Continue doing this until the matrix is completely filled.
To determine whether the matrix is completely filled, you can check whether all 4 directions all move to elements that are already filled, but an easier approach is to simply count the number of filled cells and stop if it equals the size*size of the matrix.
在“圆形”矩阵中,它的“中间”也是圆形的,
只不过它不是以 1 开头。
所以绕着周边跑并递归。
In a "circular" matrix, the "middle" of it is circular too,
except that it doesn't start with 1.
So run round the perimeter and recurse.