Incremental union?
I'm trying to describe a C ++ Sudoku Advice with a union assertion:
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];
}
Does each section of the board correspond to the correct part of the array? IE,
Will sec4 match board [4-6] [0-3]? Is there a better way to do things like this (specifically the description of the sudoku board)?
a source to share
You can achieve the desired effect 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 if this is the best way to represent sudoku. You may find that once you start working on your logic, you will get a better idea.
a source to share
It won't work as written.
The union behaves as if all of its members were at offset 0 from the start of the union.
This means it sec9
will have the same offset (zero) as sec1
it thus overlaps.
I don't think it is possible to do this with a union, since you will have to say that there is a certain "skip" that must occur after the end of each section in order to move to the next cell in that section. You cannot do this using C or C ++ arrays.
a source to share
The answer is no, the memory layout will not be what you expect. Please note, whenever you define an array in C / C ++ the memory is contiguous, so for a 9x9 array, the 4th element is not the first element of the second row, but the first element of the first row of the second 3x3 block.
Your union's memory scheme will have blocks sec
on each of the 9 lines of the object full
.
a source to share
Each member of the union, thus, every sec1..sec9 very seconds will be in one place. You can try to wrap all the sects in the structure, but still they won't match 3x3 squares, but 9 * 1 lines in the 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 summarize, real class would be the best approach.
a source to share
You are not going to solve this problem exclusively by using the capabilities of the C ++ language - you need to think about the data structure and operations on this structure that are necessary to solve your problem, in other words, you need to develop a class (or, most likely, several classes) ... This is an interesting piece of programming, so I'm not going to suggest a solution.
Another solution (besides Ferruccio's suggestion) could be to define arrays of 3 pointers to ints - one for each section and accordingly initialize these arrays 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, the accessor methods are probably much better.
a source to share
This won't work because each 3 * 3 area on a 9 * 9 board will still take up non-contiguous memory.
What I've done:
Cell grid[9][9];
Cell* cell_ptr[3][81]; //0 = by rows; 1 = by columns; 2 = by box
where is cell_ptr
filled with pointers to the grid, so it cell_ptr[0][0...80]
allows you to iterate over rows, cell_ptr[1][0...80]
will iterate over columns, and cell_ptr[2][0...80]
allows you to iterate over 3 * 3 regions.
a source to share