Dividing a 9x9 2d array into 9 sub-frames (e.g. Sudoku)? (C ++)

I am trying to encode a sudoku solution, and the way I tried to do this was to have a 9x9 pointer grid that contains the address of the β€œinstalled” objects that have either a solution or valid possible values.

I managed to go through an array with 2 for loops, through each column first, and then move on to the next row and repeat.

However, it is difficult for me to imagine how I will determine which subsegment (or field, block, etc.) belongs to a particular cell. My initial impression was to have if statements in for loops, for example, if line <2 (lines start at 0) and col <2, then we are in block 1, but that seems to be confused. Would there be a better way to do this?

+1
source share
3 answers

You can calculate the block number from the row and column as follows:

int block = (row/3)*3 + (col/3); 

This is the number of blocks, such as:

 +---+---+---+ | 0 | 1 | 2 | +---+---+---+ | 3 | 4 | 5 | +---+---+---+ | 6 | 7 | 8 | +---+---+---+ 
+7
source

I would create a lookup table with 81 entries. Each entry refers to a cell in a 9x9 grid and gives you the necessary information (in which field, in which column, in which row ...)

0
source

I use this myself (but then in python, assuming x and y go from 0 to 9 exclusive):

 int bx, by; for (bx = (x/3)*3; bx < (x/3)*3 + 3; bx++) { for (by = (y/3)*3; by < (y/3)*3 + 3; by++) { // bx and by will now loop over each number in the block which also contains x, y } } 
0
source

Source: https://habr.com/ru/post/1439694/


All Articles