Tutorialspoint
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)
NumberBacktracking FacebookPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Create a recursive function to solve the Sudoku board.
 Step 2: Iterate through each cell of the 9x9 grid.
 Step 3: When an empty cell is found (marked with '.'), try placing each digit from '1' to '9'.
 Step 4: For each digit, check if it's valid in the current position by examining the row, column, and 3x3 box.
 Step 5: If the digit is valid, place it in the cell and recursively try to solve the rest of the board.
 Step 6: If the recursive call is successful, the puzzle is solved.
 Step 7: If the recursive call is not successful, backtrack by resetting the cell to '.' and try the next digit.

Submitted Code :