在数独求解器解释上阻止扫描

时间:2014-12-29 07:33:34

标签: php algorithm sudoku

我尝试了这个数独求解器http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/。它工作正常。

另请参阅此电子表格以查看计算中的更多详细信息:spreadsheet

我理解行和列扫描如何。从董事会,我们知道:

  • index = 9 * row + col
  • row = floor(index / 9)
  • col = index%9

对于块,因为它是从3x3板构建的,所以公式为:block_index = 3*row_block + col_row。因为每个块有3行和列,所以row_block = floor(row/3)col_block = floor(col/3)的公式。从这里我们可以得出结论:

block_index = 3*row_block + col_row
block_index = 3(floor(row/3)) + floor(col/3)
block_index = floor(row/3)*3 + floor(col/3)

这可以解释这些功能:

  • return_row
  • retun_col
  • return_block
  • is_possible_row
  • is_possible_col

但我无法理解is_possible_block功能。这是功能:

function is_possible_block($number,$block,$sudoku){
    $possible = true;
    for($x = 0;$x <= 8;$x++){
        if($sudoku[floor($block / 3) * 27 + $x % 3 + 9 * floor($x / 3) + 3 * ($block % 3)] == $number){
            $possible = false;
        }
    }
    return $possible;
}

我对is_possible_block功能的了解是:

floor($block / 3) * 27 + $x % 3 + 9 * floor($x / 3) + 3 * ($block % 3)

= 9row+col
= 9(floor($block/3)*3 + floor($x/3)) + ($x%3 + 3*($block%3))

row = 3row_block+row_x_block
floor($block/3) = row_block
    floor($x/3) = row_x_block

col = 3col_block+col_x_block

如何col = 3col_block+col_x_block,因为我知道,公式应该是这样的:col = 3row_block+col

我知道col_x_block表示0-8块的列位置。并且row_block表示0-2块上的行位置。

你如何解释这个公式?感谢

更新

现在我知道了。 floor($block/3)*33*($block%3)确定了块的左上角。然后floor($x / 3)$x % 3移动块中的每个单元格。

1 个答案:

答案 0 :(得分:0)

我在c ++中使用它

for(int k = ((x - 1) / 3) * 3 + 1; k < ((x - 1) / 3) * 3 + 4; k++

因为“x”是整数“/”操作返回int值。

也许3(楼层($ block-1/3))可以帮助你。