Check for Valid Sudoku
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
According to the above three rules - The 9X9 matrix is obviously a valid Sudoku
The above problem can be solved by checking all the three conditions which need to be satisfied for a Sudoku to be valid.
Checking unique numbers in a set:
The above problem can be simply divided to sub-problems where in each sub-problem we simply need to check the numbers in the current set whether all are valid or not.
A current set can consist of numbers from a single row of the Sudoku, or from a single column, or from a 3X3 sub-box.
To check whether all numbers in the set are unique or not we can use set STL
Divide the problem into three sub-problems
For each sub-problem
If there is no such row/column/sub-box that returns false
Then it's a valid Sudoku & return true;
Comment on outputs
For the first Sudoku, there is no violation of Sudoku rule for each row, each column, each of 9 3X3 sub-boxes. Thus it’s valid Sudoku.
For the second one there is two 9 in the last row, which violates the Sudoku rule. Thus it’s an invalid one.need an explanation for this answer? contact us directly to get an explanation for this answer