I'm implementing a sudoku solver using human way algorithm. Which have 3 constraint, different number ini row, cell and box.
I googled and I got http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/. But I cannot understand how this guy get
floor($cell / 9) for return_row function or
floor(return_row($cell) / 3) * 3 + floor(return_col($cell) / 3) for return_block.
I try to figure it out by write down the data in excel and I know there is some pattern like this :
[cell] [column] 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 0
But how did he figure it out that the formula was
$cell % 9 ?
I want to know, if I don't know the answer for the formula, how can I calculate that ? How can I determine that formula ? What method should I use?
--- UPDATE ---
Based on @peterwhy answer now I understand about the row, col and block.
I know about
is_possible_row , which show that
index = 9*row + col so from here we get
row = floor(index/9) and
col = index % 9.
Now I confused about
is_possible_block function. @peterwhy said :
floor($block/3)*27 + 9*floor($x/3) is 9 times the number of rows before the cell, $x%3 + 3*($block%3) is the number of columns before the cell.
Which make me think like this:
9row+col 9(floor($block/3)*3 + floor($x/3)) + ($x%3 + 3*($block%3)) row = 3row_block+row_x_block 9(3(floor($block/3) + floor($x/3))) + ($x%3 + 3*($block%3)) floor($block/3) = row_block floor($x/3) = row_x_block col = 3col_block+col_x_block
col = 3col_block+col_x_block , because what I know, the formula should be like this :
col = 3row_block+col.
col_x_block mean column position on 0-8 block. And
row_block mean row position on 0-2 block.
Please see this to see the detail calculation.
How do you explain this?