我正在尝试使用union语句在C++中描述一个Sudoku Board:
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];
}
Run Code Online (Sandbox Code Playgroud)
电路板的每个部分是否与阵列的正确部分相对应?IE浏览器,
sec4会与棋盘[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];
};
Run Code Online (Sandbox Code Playgroud)
但是,我不确定这是代表数独棋盘的最佳方式.你可能会发现,一旦你开始研究逻辑,你就会想出一个更好的表示.