|
| 1 | +// Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: |
| 2 | + |
| 3 | +// Each row must contain the digits 1-9 without repetition. |
| 4 | +// Each column must contain the digits 1-9 without repetition. |
| 5 | +// Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition. |
| 6 | + |
| 7 | +// Note: |
| 8 | + |
| 9 | +// A Sudoku board (partially filled) could be valid but is not necessarily solvable. |
| 10 | +// Only the filled cells need to be validated according to the mentioned rules. |
| 11 | + |
| 12 | + |
| 13 | +// Example 1: |
| 14 | + |
| 15 | +/* Input: board = |
| 16 | +* [["5","3",".",".","7",".",".",".","."] |
| 17 | +* ,["6",".",".","1","9","5",".",".","."] |
| 18 | +* ,[".","9","8",".",".",".",".","6","."] |
| 19 | +* ,["8",".",".",".","6",".",".",".","3"] |
| 20 | +* ,["4",".",".","8",".","3",".",".","1"] |
| 21 | +* ,["7",".",".",".","2",".",".",".","6"] |
| 22 | +* ,[".","6",".",".",".",".","2","8","."] |
| 23 | +* ,[".",".",".","4","1","9",".",".","5"] |
| 24 | +* ,[".",".",".",".","8",".",".","7","9"]] |
| 25 | +*/ |
| 26 | +// Output: true |
| 27 | + |
| 28 | +// Example 2: |
| 29 | + |
| 30 | +/* Input: board = |
| 31 | +* [["8","3",".",".","7",".",".",".","."] |
| 32 | +* ,["6",".",".","1","9","5",".",".","."] |
| 33 | +* ,[".","9","8",".",".",".",".","6","."] |
| 34 | +* ,["8",".",".",".","6",".",".",".","3"] |
| 35 | +* ,["4",".",".","8",".","3",".",".","1"] |
| 36 | +* ,["7",".",".",".","2",".",".",".","6"] |
| 37 | +* ,[".","6",".",".",".",".","2","8","."] |
| 38 | +* ,[".",".",".","4","1","9",".",".","5"] |
| 39 | +* ,[".",".",".",".","8",".",".","7","9"]] |
| 40 | +*/ |
| 41 | +// Output: false |
| 42 | +// Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. |
| 43 | + |
| 44 | + |
1 | 45 | class Solution { |
2 | 46 | bool validate(vector<vector<char>> &board,int direct,int op){ |
3 | 47 | bool visited[9]; |
|
0 commit comments