Tutorialspoint
Problem
Solution
Submissions

Valid Sudoku Puzzles

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 20

Write a C++ program to count the number of valid Sudoku puzzle configurations possible for a given partially filled 9x9 grid.

Example 1
  • Input:
    5 3 0 0 7 0 0 0 0
    6 0 0 1 9 5 0 0 0
    0 9 8 0 0 0 0 6 0
    8 0 0 0 6 0 0 0 3
    4 0 0 8 0 3 0 0 1
    7 0 0 0 2 0 0 0 6
    0 6 0 0 0 0 2 8 0
    0 0 0 4 1 9 0 0 5
    0 0 0 0 8 0 0 7 9
  • Output: 1
  • Explanation:
    • The given partial puzzle has exactly one valid completion.
Example 2
  • Input:
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0
  • Output: 6670903752021072936960
  • Explanation:
    • An empty grid has this many valid Sudoku configurations.
Constraints
  • Grid size = 9x9
  • Each cell contains 0 (empty) or 1-9
  • At least one valid solution guaranteed
  • Time Complexity: O(9^n) where n = number of empty cells
  • Space Complexity: O(n)
Bitwise OperationsBacktracking DeloitteGoldman Sachs
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 explore all possibilities
  • Check row, column, and box validity for each move
  • Use pruning and bit manipulation to improve speed
  • Consider using bit manipulation for checking valid numbers.
  • For large counts, use appropriate data types to handle big integers.

Steps to solve by this approach:


Step 1: Set up data structures to track digits in rows, columns, and boxes
Step 2: Initialize constraints from the given board
Step 3: Apply recursive backtracking to fill in the board
Step 4: Check digit placement validity at each step
Step 5: Count every valid complete configuration
Step 6: Return the final count

Submitted Code :