|
| 1 | +/* |
| 2 | +Determine if a Sudoku is valid. |
| 3 | +The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. |
| 4 | +
|
| 5 | +The input corresponding to the above configuration : |
| 6 | +53..7.... |
| 7 | +6..195... |
| 8 | +.98....6. |
| 9 | +8...6...3 |
| 10 | +4..8.3..1 |
| 11 | +7...2...6 |
| 12 | +.6....28. |
| 13 | +...419..5 |
| 14 | +....8..79 |
| 15 | +*/ |
| 16 | +#include <bits/stdc++.h> |
| 17 | + |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +int is_Valid_Rows_And_Columns(vector<vector<char> > &sudokuBoard) |
| 21 | +{ |
| 22 | + |
| 23 | + for (int r = 0; r < 9; ++r) |
| 24 | + { |
| 25 | + unordered_set<char> row_set; |
| 26 | + for (int c = 0; c < 9; ++c) |
| 27 | + { |
| 28 | + |
| 29 | + int cell_Value=sudokuBoard[r][c]; |
| 30 | + if(cell_Value=='.') |
| 31 | + { |
| 32 | + continue; |
| 33 | + } |
| 34 | + // if(cell_Value < '1' || cell_Value >'9') |
| 35 | + // return 0; |
| 36 | + if(row_set.find(cell_Value)!=row_set.end()) |
| 37 | + { |
| 38 | + return 0; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + row_set.insert(cell_Value); |
| 43 | + } |
| 44 | + } |
| 45 | + row_set.clear(); |
| 46 | + } |
| 47 | + for (int c = 0; c < 9; ++c) |
| 48 | + { |
| 49 | + unordered_set<char> col_set; |
| 50 | + for (int r = 0; r < 9; ++r) |
| 51 | + { |
| 52 | + int cell_Value=sudokuBoard[r][c]; |
| 53 | + if(cell_Value=='.') |
| 54 | + { |
| 55 | + continue; |
| 56 | + } |
| 57 | + // if(cell_Value < '1' || cell_Value >'9') |
| 58 | + // return 0; |
| 59 | + if(col_set.find(cell_Value)!=col_set.end()) |
| 60 | + { |
| 61 | + return 0; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + col_set.insert(cell_Value); |
| 66 | + } |
| 67 | + } |
| 68 | + col_set.clear(); |
| 69 | + } |
| 70 | + return 1; |
| 71 | +} |
| 72 | + |
| 73 | +int is_Valid_blocks(vector<vector<char> > &sudokuBoard) |
| 74 | +{ |
| 75 | + unordered_set<char> block_set; |
| 76 | + |
| 77 | + for (int row_block = 0; row_block < 3; ++row_block) |
| 78 | + { |
| 79 | + for (int col_block = 0; col_block < 3; ++col_block) |
| 80 | + { |
| 81 | + unordered_set<char> block_set; |
| 82 | + |
| 83 | + for (int min_row = 0; min_row < 3; ++min_row) |
| 84 | + { |
| 85 | + for (int min_col = 0; min_col < 3; ++min_col) |
| 86 | + { |
| 87 | + int r=row_block*3+min_row; |
| 88 | + int c=col_block*3+min_col; |
| 89 | + int cell_Value=sudokuBoard[r][c]; |
| 90 | + |
| 91 | + if(cell_Value=='.') |
| 92 | + { |
| 93 | + continue; |
| 94 | + } |
| 95 | + // if(cell_Value < '1' || cell_Value >'9') |
| 96 | + // return 0; |
| 97 | + if(block_set.find(cell_Value)!=block_set.end()) |
| 98 | + { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + block_set.insert(cell_Value); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + block_set.clear(); |
| 108 | + } |
| 109 | + } |
| 110 | + return 1; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +int is_Valid(vector<vector<char> > &sudokuBoard) |
| 116 | +{ |
| 117 | + if (!is_Valid_Rows_And_Columns(sudokuBoard)) |
| 118 | + { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + if(!is_Valid_blocks(sudokuBoard)) |
| 122 | + { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + return 1; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +int main() |
| 133 | +{ |
| 134 | + |
| 135 | + vector<string> A; |
| 136 | + vector<vector<char> > sudokuBoard(9,vector<char> (9)); |
| 137 | + string x; |
| 138 | + for (int i = 0; i < 9; ++i) |
| 139 | + { |
| 140 | + cin >> x; |
| 141 | + A.push_back(x); |
| 142 | + } |
| 143 | + |
| 144 | + for (int i = 0; i < 9; ++i) |
| 145 | + { |
| 146 | + int k=0; |
| 147 | + while(A[i][k]!='\0') |
| 148 | + { |
| 149 | + sudokuBoard[i][k]=A[i][k]; |
| 150 | + k++; |
| 151 | + } |
| 152 | + } |
| 153 | + int ans=is_Valid(sudokuBoard); |
| 154 | + cout << ans <<endl; |
| 155 | +} |
0 commit comments