You're given a solution to a Sudoku puzzle. Write the code to check if it's a valid solution.
Your function signature should be:
boolean isValid(int starti, int startj, int endi, int endj)
Rules for those unfamiliar with Sudoku:
- Grid size is 9x9, divided into 9 regions of 3x3
- Each row must contain all digits from 1-9
- Each column must contain all digits from 1-9
- Each 3x3 square must contain all digits from 1-9
I wasn't asked this question, but saw it on several places. Checking the last rule might be the interesting part
I had a similar task, input coming from standard input.
I used sets. Put the element to the right row, column, and square (called box in my code). If all 3 set's size is 9, it's valid.
}
the solution which use just one array iteration. through the whole iteration of array, posRec array is going to have each numbers availability in ROW/COL/GRID. if there is missing bit in posRec, we can say sudoku isn’t correct.
The rows and columns can be validated in a single loop
}
Sorry, I know this must be homework, but I can't help myself. It's just too much fun to come up with something :-)
A spoon full of LINQ makes the medicine go down:
The nice thing about this solution is, that no teacher will believe you if you say you came up with this ;-)
In reference to the excellent answer from @Stephen, here is a solution in C# without resorting to LINQ, to show the difference between the two approaches.
This solution handles both 4x4 and 9x9 Sodoku boards.