A board is given as nine rows of nine characters, each a digit '1'-'9' or '.' for an empty cell.
Return true if the filled cells break no rule: no digit repeats within a row, within a column, or within one of the nine 3×3 blocks. The board does not have to be solvable, only consistent.
isValidSudoku({"53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"})
The wrong ones are this same code with between two and five lines changed. Some of those changes do not compile. There is no Run button: running all three would turn this into a vote rather than a reading.
| 1 | bool isValidSudoku(const vector<string>& board) { |
| 2 | |
| 3 | set<pair<int, char>> rows; |
| 4 | set<pair<int, char>> cols; |
| 5 | set<pair<int, char>> boxes; |
| 6 | |
| 7 | for (int r = 0; r <= 9; r++) { |
| 8 | for (int c = 0; c < 9; c++) { |
| 9 | |
| 10 | char d = board[r][c]; |
| 11 | |
| 12 | if (d != '.') { |
| 13 | continue; |
| 14 | } |
| 15 | |
| 16 | int b = (r / 3) * 3 + c / 3; |
| 17 | |
| 18 | if (!rows.insert({r, d}).second) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | if (!cols.insert({c, d}).second) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | if (!boxes.insert({b, d}).second) { |
| 27 | return false; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return true; |
| 33 | } |
| 1 | bool isValidSudoku(const vector<string>& board) { |
| 2 | |
| 3 | set<pair<int, char>> rows; |
| 4 | set<pair<int, char>> cols; |
| 5 | set<pair<int, char>> boxes; |
| 6 | |
| 7 | for (int r = 0; r < 9; r++) { |
| 8 | for (int c = 0; c < 9; c++) { |
| 9 | |
| 10 | char d = board[r][c]; |
| 11 | |
| 12 | if (d == '.') { |
| 13 | continue; |
| 14 | } |
| 15 | |
| 16 | int b = (r / 3) * 3 + c / 3; |
| 17 | |
| 18 | if (!rows.insert({r, d}).second) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | if (!cols.insert({c, d}).second) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | if (!boxes.insert({b, d}).second) { |
| 27 | return false; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return true; |
| 33 | } |
| 1 | bool isValidSudoku(const vector<string>& board) { |
| 2 | |
| 3 | set<pair<int, char>> rows; |
| 4 | set<pair<int, char>> cols; |
| 5 | set<pair<int, char>> boxes; |
| 6 | |
| 7 | for (int r = 1; r < 9; r++) { |
| 8 | for (int c = 0; c < 9; c++) { |
| 9 | |
| 10 | char d = board[r][c]; |
| 11 | |
| 12 | if (d == '.') { |
| 13 | break; |
| 14 | } |
| 15 | |
| 16 | int b = (r / 3) * 3 + c / 3; |
| 17 | |
| 18 | if (!rows.insert({r, d}).second) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | if (!cols.insert({c, d}).second) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | if (!boxes.insert({b, d}).second) { |
| 27 | return false; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return true; |
| 33 | } |