赞
踩
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
The ‘.’ character indicates empty cells.
Input: 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”]]
Output: [[“5”,“3”,“4”,“6”,“7”,“8”,“9”,“1”,“2”],[“6”,“7”,“2”,“1”,“9”,“5”,“3”,“4”,“8”],[“1”,“9”,“8”,“3”,“4”,“2”,“5”,“6”,“7”],[“8”,“5”,“9”,“7”,“6”,“1”,“4”,“2”,“3”],[“4”,“2”,“6”,“8”,“5”,“3”,“7”,“9”,“1”],[“7”,“1”,“3”,“9”,“2”,“4”,“8”,“5”,“6”],[“9”,“6”,“1”,“5”,“3”,“7”,“2”,“8”,“4”],[“2”,“8”,“7”,“4”,“1”,“9”,“6”,“3”,“5”],[“3”,“4”,“5”,“2”,“8”,“6”,“1”,“7”,“9”]]
Explanation: The input board is shown above and the only valid solution is shown below:
From: LeetCode
Link: 29. Divide Two Integers
The isValid Function: This helper function checks if a certain number can be placed at a given row and column without breaking the Sudoku rules.
The solve Function: This is the recursive function that implements the backtracking. It tries to place every number from ‘1’ to ‘9’ in each empty cell and calls itself to try to solve the rest of the board from that point. If it eventually hits a point where no numbers can fit, it returns false, causing the previous level of recursion to try the next number.
The solveSudoku Function: This is the function you call to solve the board. It calls the solve function and passes the board to it.
In-Place Modification: The board is modified in place. This means that the original 2D array of characters that you pass to the solveSudoku function will be changed to reflect the solution to the puzzle by the time the function returns. There is no need to print or return the board from the function because it directly modifies the array you pass to it.
Guaranteed Solution: The problem statement you provided guarantees that there is one and only one solution for the input board, so the code does not handle multiple solution scenarios or no-solution scenarios.
bool isValid(char** board, int row, int col, char num){ for(int i = 0; i < 9; i++) { // Check if the number is already used in the current row or column if(board[row][i] == num || board[i][col] == num) return false; // Check if the number is in the current 3x3 box if(board[3*(row/3) + i/3][3*(col/3) + i%3] == num) return false; } return true; } bool solve(char** board) { for(int row = 0; row < 9; row++) { for(int col = 0; col < 9; col++) { // Skip non-empty cells if(board[row][col] != '.') continue; for(char num = '1'; num <= '9'; num++){ // If placing the current num in board[row][col] doesn't violate Sudoku rules if(isValid(board, row, col, num)) { board[row][col] = num; // Recursively proceed to fill the rest of the board if(solve(board)) return true; // If the decision leads to a dead end, undo it board[row][col] = '.'; } } // If no number can be placed in this cell, backtrack return false; } } // If all cells are filled, the puzzle is solved return true; } void solveSudoku(char** board, int boardSize, int* boardColSize) { solve(board); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。