Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <windows.h>
- using namespace std;
- #define ii pair<int,int>
- #define fi first
- #define sc second
- #define map candidate
- #define cnt count
- int sol[9][9];
- set<int> map[9][9];
- vector<int> tab[3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
- int main() {
- freopen("in" , "r", stdin );
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hConsole, 12 + 224);
- cout << " imilinh emulator . " << "\n\n";
- SetConsoleTextAttribute(hConsole, 15);
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- cin >> sol[i][j];
- if (sol[i][j]) map[i][j].insert(sol[i][j]);
- else map[i][j] = set<int>({1, 2, 3, 4, 5, 6, 7, 8, 9});
- }
- int loop = 100;
- while (loop --> 0) {
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- if (sol[i][j]) {
- for (int k = 0; k < 9; k++) {
- if (!sol[i][k]) map[i][k].erase(sol[i][j]);
- if (!sol[k][j]) map[k][j].erase(sol[i][j]);
- }
- for (auto k : tab[i / 3])
- for (auto l : tab[j / 3]) {
- if (!sol[k][l]) map[k][l].erase(sol[i][j]);
- }
- }
- }
- // ----- naked single -----
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- if (map[i][j].size() == 1) sol[i][j] = *map[i][j].begin();
- }
- // ------------------------
- // ----- hidden single ----
- vector<multiset<int>> row(9), col(9);
- vector<vector<multiset<int>>> stb(3, vector<multiset<int>>(3));
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- for (auto k : map[i][j]) {
- row[i].insert(k);
- col[j].insert(k);
- stb[i / 3][j / 3].insert(k);
- }
- }
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- for (int k = 1; k <= 9; k++) {
- int t = row[i].cnt(k) * col[j].cnt(k) * stb[i / 3][j / 3].cnt(k);
- if (map[i][j].cnt(k) && t == 1) {
- sol[i][j] = k;
- map[i][j].clear();
- map[i][j].insert(k);
- break;
- }
- }
- }
- // ------------------------
- // ----- pointing ---------
- for (int k = 1; k <= 9; k++) {
- vector<vector<vector<ii>>> ptb(3, vector<vector<ii>>(3));
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- if (map[i][j].cnt(k)) ptb[i / 3][j / 3].push_back(ii(i, j));
- }
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++) {
- if (ptb[i][j].size()) {
- int r = ptb[i][j].begin()->fi;
- int c = ptb[i][j].begin()->sc;
- bool br = 1, bc = 1;
- for (auto t : ptb[i][j]) {
- if (t.fi != r) br = 0;
- if (t.sc != c) bc = 0;
- }
- for (int l = 0; l < 9; l++) {
- if (br && l / 3 != j) map[r][l].erase(k);
- if (bc && l / 3 != i) map[l][c].erase(k);
- }
- }
- }
- }
- // ------------------------
- // ----- claiming ---------
- for (int k = 1; k <= 9; k++) {
- vector<vector<int>> rw(9), cl(9);
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- if (map[i][j].cnt(k)) {
- rw[i].push_back(j);
- cl[j].push_back(i);
- }
- }
- for (int l = 0; l < 9; l++) {
- if (rw[l].size()) {
- int c = rw[l].front();
- bool br = 1;
- for (auto t : rw[l]) if (t / 3 != c / 3) br = 0;
- if (br) {
- for (auto h : tab[l / 3])
- for (auto g : tab[c / 3]) {
- if (h != l) map[h][g].erase(k);
- }
- }
- }
- if (cl[l].size()) {
- int r = cl[l].front();
- bool bc = 1;
- for (auto t : cl[l]) if (t / 3 != r / 3) bc = 0;
- if (bc) {
- for (auto h : tab[r / 3])
- for (auto g : tab[l / 3]) {
- if (g != l) map[h][g].erase(k);
- }
- }
- }
- }
- }
- // ------------------------
- // ----- naked pair -------
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- if (map[i][j].size() == 2) {
- for (int k = 0; k < 9; k++) {
- if (k != j && map[i][j] == map[i][k]) {
- for (int l = 0; l < 9; l++) {
- if (l != j && l != k) {
- for (auto t : map[i][j]) map[i][l].erase(t);
- }
- }
- }
- if (k != i && map[i][j] == map[k][j]) {
- for (int l = 0; l < 9; l++) {
- if (l != i && l != k) {
- for (auto t : map[i][j]) map[l][j].erase(t);
- }
- }
- }
- }
- for (auto k : tab[i / 3])
- for (auto l : tab[j / 3]) {
- if (k != i && j != l && map[i][j] == map[k][l]) {
- for (auto h : tab[i / 3])
- for (auto g : tab[j / 3]) {
- if (!((h == i && g == j) || (h == k && g == l))) {
- for (auto t : map[i][j]) map[h][g].erase(t);
- }
- }
- }
- }
- }
- }
- // ------------------------
- // ----- hidden pair ------
- for (int k = 1; k <= 9 - 1; k++)
- for (int l = k + 1; l <= 9; l++) {
- vector<vector<int>> ro(9), co(9);
- vector<vector<vector<ii>>> ta(3, vector<vector<ii>>(3));
- vector<bool> fro(9, 1), fco(9, 1);
- vector<vector<bool>> fta(3, vector<bool>(3, 1));
- for (int i = 0; i < 9; i++)
- for (int j = 0; j < 9; j++) {
- if (map[i][j].cnt(k) && map[i][j].cnt(l)) {
- ro[i].push_back(j);
- co[j].push_back(i);
- ta[i / 3][j / 3].push_back(ii(i, j));
- } else {
- if (map[i][j].cnt(k) || map[i][j].cnt(l)) {
- fro[i] = 0;
- fco[j] = 0;
- fta[i / 3][j / 3] = 0;
- }
- }
- }
- for (int i = 0; i < 9; i++) {
- if (ro[i].size() == 2 && fro[i]) {
- for (auto j : ro[i]) {
- for (int t = 1; t <= 9; t++) {
- if (t != k && t != l) map[i][j].erase(t);
- }
- }
- }
- }
- for (int j = 0; j < 9; j++) {
- if (co[j].size() == 2 && fco[j]) {
- for (auto i : co[j]) {
- for (int t = 1; t <= 9; t++) {
- if (t != k && t != l) map[i][j].erase(t);
- }
- }
- }
- }
- for (int h = 0; h < 3; h++)
- for (int g = 0; g < 3; g++) {
- if (ta[h][g].size() == 2 && fta[h][g]) {
- for (auto p : ta[h][g]) {
- for (int t = 1; t <= 9; t++) {
- if (t != k && t != l) map[p.fi][p.sc].erase(t);
- }
- }
- }
- }
- }
- // ------------------------
- }
- for (int i = -1; i < 9; i++) {
- if (i != -1) {
- cout << "| ";
- for (int j = 0; j < 9; j++) {
- int colour = 0;
- switch (sol[i][j]) {
- case 1: colour = 10; break;
- case 2: colour = 14; break;
- case 3: colour = 4; break;
- case 4: colour = 12; break;
- case 5: colour = 6; break;
- case 6: colour = 1; break;
- case 7: colour = 5; break;
- case 8: colour = 8; break;
- case 9: colour = 9; break;
- }
- SetConsoleTextAttribute(hConsole, colour);
- if (!sol[i][j]) cout << ' ';
- else cout << sol[i][j];
- SetConsoleTextAttribute(hConsole, 15);
- if (j == 8) cout << " |\n";
- else if ((j + 1) % 3 == 0) cout << " | "; else cout << " ";
- }
- }
- if ((i + 1) % 3 == 0) {
- for (int j = 0; j <= 30; j++) {
- if (j % 10 == 0) cout << '+'; else cout << '-';
- }
- cout << '\n';
- }
- }
- cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement