我试图用 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] 吗?有没有更好的方法来做这类事情(特别是描述数独板)?
最佳答案
你可以通过将它封装在一个类中来实现你想要的效果:
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];
};
但是,我不确定这是表示数独棋盘的最佳方式。您可能会发现,一旦您开始研究逻辑,您就会想出更好的表示。
关于c++ - 增量联盟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2682408/