darwinrlo / public

0 stars 0 forks source link

Sudoku Solver #28

Open darwinrlo opened 4 years ago

darwinrlo commented 4 years ago

Mapping a cell to a sub-box

There are nine sub-boxes and we can refer to them using 2-D coordinates. For example, the second box on the third row of boxes can be referred to as sub-box (2, 1). In general, the sub-boxes can be identified using a 2-D coordinate from { 0, 1, 2 } x { 0, 1, 2 }.

It is straightforward to map a cell (r, c) to a sub-box's 2-D coordinate: We can simply divide both coordinates by 3. That is, cell (r, c) is in sub-box (r/3, c/3). For example, cell (5, 7) is in sub-box (1, 2).

If we want, we can map each sub-box to the numbers 0, ..., 8. Notice that, if the first coordinate is 0, then the second coordinate reflects the sub-box number, which is either 0, 1, or 2. If the first coordinate is 1, you have to add 3 to the second coordinate. If the first coordinate is 2, you have to add 6. So sub-box (x, y) is sub-box 3*x + y.

Finally, if instead, we want to map the sub-boxes to 1, ..., 9, we can just add 1 to the previously computed sub-box number.