增量联盟?
我试图用 union 语句描述 C++ 中的数独板:
union Board
{
int board[9][9];
int sec1[3][3];
int sec2[3][3];
int sec3[3][3];
int sec4[3][3];
int sec5[3][3];
int sec6[3][3];
int sec7[3][3];
int sec8[3][3];
int sec9[3][3];
}
板的每个部分是否与数组的正确部分相对应?即,
sec4 与 board[4-6][0-3] 对应吗?有没有更好的方法来做这种事情(特别是描述数独板)?
I'm trying to describe a Sudoku Board in C++ with a union statement:
union Board
{
int board[9][9];
int sec1[3][3];
int sec2[3][3];
int sec3[3][3];
int sec4[3][3];
int sec5[3][3];
int sec6[3][3];
int sec7[3][3];
int sec8[3][3];
int sec9[3][3];
}
Would each section of the board correspond with the correct part of the array? IE,
Would sec4 correspond with board[4-6][0-3]? Is there a better way to do this sort of thing (specifically describing a sudoku board)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以通过将其封装在一个类中来实现您想要的效果:
但是,我不确定这是表示数独板的最佳方式。您可能会发现,一旦开始研究逻辑,您就会想出更好的表示方法。
You could achieve the effect you want by encapsulating it in a class:
but, I'm not sure this is the best way to represent a Sudoku board. You may find that once you start working on the logic, you'll come up with a better representation.
答案是否定的,内存布局不会是你所期望的。请注意,每当您在 C/C++ 中定义数组时,内存都是连续的,因此对于 9x9 数组,第四个元素不是第二行的第一个元素,而是第二个 3x3 块的第一行的第一个元素。
联合的内存布局将在
full
对象的 9 行中的每一行上都有sec
块。The answer is no, the memory layout will not be what you expect. Note that whenever you define an array in C/C++ the memory is contiguous, so for the 9x9 array the 4th element is not the first element of the second row, but rather the first element of the first row of the second 3x3 block.
The memory layout of your union will have the
sec
blocks over each one of the 9 lines of thefull
object.正如所写,这是行不通的。
联合的行为就好像它的所有成员都位于从联合开始的偏移量 0 处。
这意味着
sec9
将具有与sec1
相同的偏移量(零),从而重叠。我认为也不可能使用联合来做到这一点,因为您需要表示在每个部分结束后需要发生一定的“跳过”,以到达该部分中的下一个单元格。使用 C 或 C++ 数组无法做到这一点。
This won't work, as written.
A union behaves as if all its members are at offset 0 from the union's start.
That means that
sec9
will have the same offset (zero) assec1
, thus overlap.I don't think it's possible to do it using a union either, since you would need to express that there's a certain "skip" that needs to happen after the end of each section, to get to the next cell in that section. You can't do that using C or C++ arrays.
每个联合成员,因此每个 sec1..sec9 秒都将位于相同的位置。您可以尝试将所有宗派包装在一个结构中,但它们仍然不会对应于 3x3 正方形,而是对应于原始结构中的 9*1 行:
总而言之,真正的类将是最好的方法。
Every union member, thus every sec1..sec9 very sec will be in the same location. You could try wrap all sects in a struct, but still they will not correspond to 3x3 squares, but rather 9*1 rows in original structure:
To sum up, real class would be the best approach.
您不会纯粹使用 C++ 语言功能来解决这个问题 - 您需要考虑解决问题所需的数据结构和该结构上的操作 - 换句话说,您需要设计一个类(或更可能是多个类) 。这是编程的有趣部分,所以我不会提出解决方案。
You are not going to solve this purely using C++ language features - you need to think about the data structure and operations on that structure that are needed to solve your problem - in other words you need to design a class (or more likely several classes). This is the fun part of programming, so I'm not going to suggest a solution.
另一种解决方案(除了 Ferruccio 提出的方案之外)可能是定义 3 个指向 int 的指针的数组 - 每个部分一个,并在构造函数中相应地初始化这些数组。
但坦率地说,访问方法可能要好得多。
Another solution (besides the one proposed by Ferruccio) could be to define arrays of 3 pointers to ints - one for each section, and initialise those arrays accordingly in the constructor.
But frankly methods for access are probably much better.
这是行不通的,因为 9 * 9 板上的每个 3 * 3 区域无论如何都会占用非连续内存。
我所做的是:
其中
cell_ptr
填充有指向网格的指针,以便cell_ptr[0][0...80]
允许迭代行,cell_ptr[1][0...80]
将迭代列,而cell_ptr[2][0...80]
允许迭代 3 * 3 区域。That will not work, because each 3 * 3 region in the 9 * 9 board will be occupying non-contiguous memory anyway.
What I have done is:
where
cell_ptr
is populated with pointers into the grid so thatcell_ptr[0][0...80]
allows iterating over rows,cell_ptr[1][0...80]
will be iterating over columns, andcell_ptr[2][0...80]
allows iterating over 3 * 3 regions.