37. Sudoku Solver #2118
-
Topics: Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules:
The Example 1:
Example 2:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to optimize the Sudoku solver by reducing the time complexity of validity checks. The previous approach checked each digit's validity by scanning the entire row, column, and 3x3 box, which is inefficient. Instead, we can use arrays to keep track of which digits are already present in each row, column, and box. This allows us to check the validity of a digit in constant time. Approaches
Let's implement this solution in PHP: 37. Sudoku Solver <?php
/**
* @param String[][] $board
* @return NULL
*/
function solveSudoku(&$board) {
$rows = array_fill(0, 9, array_fill(0, 10, false));
$cols = array_fill(0, 9, array_fill(0, 10, false));
$boxes = array_fill(0, 9, array_fill(0, 10, false));
// Initialize the arrays based on the current board
for ($i = 0; $i < 9; $i++) {
for ($j = 0; $j < 9; $j++) {
if ($board[$i][$j] != '.') {
$num = (int)$board[$i][$j];
$boxIndex = intdiv($i, 3) * 3 + intdiv($j, 3);
$rows[$i][$num] = true;
$cols[$j][$num] = true;
$boxes[$boxIndex][$num] = true;
}
}
}
solve($board, 0, $rows, $cols, $boxes);
}
/**
* @param $board
* @param $index
* @param $rows
* @param $cols
* @param $boxes
* @return bool
*/
private function solve(&$board, $index, &$rows, &$cols, &$boxes) {
if ($index == 81) {
return true;
}
$row = intdiv($index, 9);
$col = $index % 9;
if ($board[$row][$col] != '.') {
return solve($board, $index + 1, $rows, $cols, $boxes);
}
$boxIndex = intdiv($row, 3) * 3 + intdiv($col, 3);
for ($c = 1; $c <= 9; $c++) {
if (!$rows[$row][$c] && !$cols[$col][$c] && !$boxes[$boxIndex][$c]) {
$board[$row][$col] = (string)$c;
$rows[$row][$c] = true;
$cols[$col][$c] = true;
$boxes[$boxIndex][$c] = true;
if (solve($board, $index + 1, $rows, $cols, $boxes)) {
return true;
}
$board[$row][$col] = '.';
$rows[$row][$c] = false;
$cols[$col][$c] = false;
$boxes[$boxIndex][$c] = false;
}
}
return false;
}
// Test cases
// Example 1
$board = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
];
solveSudoku($board);
// Print solved board
foreach ($board as $row) {
echo implode(" ", $row) . PHP_EOL;
}
// Example 2
$board = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
];
solveSudoku($board);
// Print solved board
foreach ($board as $row) {
echo implode(" ", $row) . PHP_EOL;
}
?> Explanation:
This optimized approach significantly reduces the time complexity of validity checks, making the solution efficient enough to handle the constraints. |
Beta Was this translation helpful? Give feedback.
We need to optimize the Sudoku solver by reducing the time complexity of validity checks. The previous approach checked each digit's validity by scanning the entire row, column, and 3x3 box, which is inefficient. Instead, we can use arrays to keep track of which digits are already present in each row, column, and box. This allows us to check the validity of a digit in constant time.
Approaches
$rows
,$cols
,$boxes
) to keep track of digits present in each row, column, and 3x3 box.