如何恢复对角读取的 NxN 矩阵?
假设如果我有一个二维数组
,我得到一个数组“char input[N][N]”,它被某人对角读取。
我的目标是从对角读取的“char input[N][N]”数组中重现原始数组“char input_original[N][N]”
对角读取后,我的输入是,
adb
ceg -------- ----------->矩阵 A
h fi
现在我想将其设为
abc
def ------------------->矩阵 B (input_original array)
ghi
其中矩阵 A 是矩阵 B 的对角读取输出( a db ceg hf i --> 将其作为矩阵)
欢迎所有建议。
Suppose if am having one 2D array
I got an array , 'char input[N][N]' which was read diagonally by someone.
My aim is reproduce the original array 'char input_original[N][N]' from the diagonally read 'char input[N][N]' array
After reading diagonally , my input is ,
a d b
c e g -------------------> matrix A
h f i
and now i want to make it as
a b c
d e f -------------------> matrix B (input_original array)
g h i
Where the matrix A is the diagonal read output( a db ceg hf i --> made this as a matrix) of matrix B
All suggestions are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果矩阵是对角线的,我只会计算通过矩阵的“路径”,例如
,您按照读取索引的顺序存储索引。完成此操作后,您可以根据这些索引填充矩阵。
所以这种情况下最困难的就是如何计算路径。您可以按如下方式执行此操作: 首先,我们认识到该路径仅由四个方向组成:
此外,我们认识到只有某些方向顺序是允许:
使用这些知识计算路径是没有问题的:我们只需要存储最后一个方向和当前位置。然后,我们必须按以下方式计算下一个方向:
我希望我没有搞砸任何事情,但我认为这是一个非常实用的方法。
I would just compute the "path" through your matrix if it's diagonal, e.g.
This is, you store the indices in the order you read them in. After you've done this, you can just fill your matrix according to these indices.
So the most difficult stuff in this case is how to compute the path. You can do this as follows: First of all, we recognize that the path conists exclusively of four directions:
Furthermore we recognize that only some orders of directions are allowed:
Using this knowledge it is no problem to compute the path: We just have to store the last direction and the current position. Then, we have to compute the next direction the following way:
I hope I didn't mess anything up, but I think that's quite a practical way to do it.