
Problem
Solution
Submissions
Sudoku Solver
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C# program to implement the SolveSudoku(char[][] board) function that solves a 9x9 Sudoku puzzle. The Sudoku board is represented as a 9x9 char array where empty cells are filled with '.' and cells with digits are filled with '1' to '9'. Your program should modify the input board directly.
Example 1
- 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']
]
Example 2
- Input: board = [
['.','.','9','7','4','8','.','.','.'],
['7','.','.','.','.','.','.','.','.'],
['.','2','.','1','.','9','.','.','.'],
['.','.','7','.','.','.','2','4','.'],
['.','6','4','.','1','.','5','9','.'],
['.','9','8','.','.','.','3','.','.'],
['.','.','.','8','.','3','.','2','.'],
['.','.','.','.','.','.','.','.','6'],
['.','.','.','2','7','5','9','.','.']
] - Output: [
['5','1','9','7','4','8','6','3','2'],
['7','8','3','6','5','2','4','1','9'],
['4','2','6','1','3','9','8','7','5'],
['3','5','7','9','8','6','2','4','1'],
['2','6','4','3','1','7','5','9','8'],
['1','9','8','5','2','4','3','6','7'],
['9','7','5','8','6','3','1','2','4'],
['8','3','2','4','9','1','7','5','6'],
['6','4','1','2','7','5','9','8','3']
]
Constraints
- board.length == 9
- board[i].length == 9
- board[i][j] is a digit '1'-'9' or '.'
- It is guaranteed that the input board has only one solution
- Time Complexity: O(9^(n*n)) where n is the board size (worst case)
- Space Complexity: O(n*n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use backtracking to solve the Sudoku puzzle
- For each empty cell, try placing digits 1-9
- Check if the digit is valid in the current position (row, column, and 3x3 box)
- If valid, place the digit and recursively try to solve the rest of the board
- If the recursive call is successful, the puzzle is solved. Otherwise, backtrack