-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidSudoku.cpp
63 lines (54 loc) · 1.53 KB
/
validSudoku.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <numeric>
bool validSudoku(std::vector<std::vector<int32_t>>& input){
using std::vector;
using std::set;
using std::map;
using std::pair;
constexpr uint32_t blockSize{3};
if (input.empty())
return false;
vector<set<int32_t>> rowSets(input.size(), set<int32_t>());
vector<set<int32_t>> colSets(input[0].size(),set<int32_t>());
map<pair<uint32_t,uint32_t>,set<int32_t>> blockSets{};
vector<uint32_t> fillVec(input.size()/blockSize, 0);
std::iota(fillVec.begin(), fillVec.end(), 0);
for(auto i : fillVec){
for (auto j : fillVec){
blockSets.insert({{i,j},{}});
}
}
uint32_t rowIdx{0};
for (auto row : input){
uint32_t colIdx{0};
for (auto col : row){
auto [it, valid] = rowSets[rowIdx].insert(col);
if(valid){
auto [it, valid] = colSets[colIdx].insert(col);
if(valid){
auto blockRowIdx{static_cast<uint32_t>(rowIdx/blockSize)};
auto blockColIdx{static_cast<uint32_t>(colIdx/blockSize)};
auto blockKey{pair<uint32_t,uint32_t>(blockRowIdx,blockColIdx)};
auto [it, valid] = blockSets[blockKey].insert(col);
if (!valid)
return valid;
}else
return valid;
}
else
return valid;
++colIdx;
}
++rowIdx;
}
return true;
}
int main(){
std::vector<std::vector<int32_t>> input{};
std::map<bool,std::string> bool2String{{true, "true"}, {false, "false"}};
bool isValid{validSudoku(input)};
std::cout<< "Is valid sudoko: " << bool2String[isValid] << std::endl;
}