增量联盟?

发布于 2024-08-30 05:02:52 字数 374 浏览 5 评论 0原文

我试图用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我一直都在从未离去 2024-09-06 05:02:52

您可以通过将其封装在一个类中来实现您想要的效果:

class Board {
public:
    int& sec1(int r, int c) { return board[r][c]); }
    int& sec2(int r, int c) { return board[r][c+3]; }
    // etc.

private:
    int board[9][9];
};

但是,我不确定这是表示数独板的最佳方式。您可能会发现,一旦开始研究逻辑,您就会想出更好的表示方法。

You could achieve the effect you want by encapsulating it in a class:

class Board {
public:
    int& sec1(int r, int c) { return board[r][c]); }
    int& sec2(int r, int c) { return board[r][c+3]; }
    // etc.

private:
    int board[9][9];
};

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.

沐歌 2024-09-06 05:02:52

答案是否定的,内存布局不会是你所期望的。请注意,每当您在 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 the full object.

小情绪 2024-09-06 05:02:52

正如所写,这是行不通的。

联合的行为就好像它的所有成员都位于从联合开始的偏移量 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) as sec1, 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.

冬天的雪花 2024-09-06 05:02:52

每个联合成员,因此每个 sec1..sec9 秒都将位于相同的位置。您可以尝试将所有宗派包装在一个结构中,但它们仍然不会对应于 3x3 正方形,而是对应于原始结构中的 9*1 行:

union Board
{
    int board[9][9];
    struct {
        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];     
    } sects;

}

总而言之,真正的类将是最好的方法。

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:

union Board
{
    int board[9][9];
    struct {
        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];     
    } sects;

}

To sum up, real class would be the best approach.

魔法少女 2024-09-06 05:02:52

您不会纯粹使用 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.

逐鹿 2024-09-06 05:02:52

另一种解决方案(除了 Ferruccio 提出的方案之外)可能是定义 3 个指向 int 的指针的数组 - 每个部分一个,并在构造函数中相应地初始化这些数组。

class Board {
public:
    int *sec1[3]; // sec1[0] = &(board[0][0]), sec1[1] = &(board[1][0]),sec1[2] = &(board[2][0])
    ...

    int board[9][9];
};

但坦率地说,访问方法可能要好得多。

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.

class Board {
public:
    int *sec1[3]; // sec1[0] = &(board[0][0]), sec1[1] = &(board[1][0]),sec1[2] = &(board[2][0])
    ...

    int board[9][9];
};

But frankly methods for access are probably much better.

我的黑色迷你裙 2024-09-06 05:02:52

这是行不通的,因为 9 * 9 板上的每个 3 * 3 区域无论如何都会占用非连续内存。

我所做的是:

Cell grid[9][9];
Cell* cell_ptr[3][81]; //0 = by rows; 1 = by columns; 2 = by box

其中 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:

Cell grid[9][9];
Cell* cell_ptr[3][81]; //0 = by rows; 1 = by columns; 2 = by box

where cell_ptr is populated with pointers into the grid so that cell_ptr[0][0...80] allows iterating over rows, cell_ptr[1][0...80] will be iterating over columns, and cell_ptr[2][0...80] allows iterating over 3 * 3 regions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文