Advertisement
tuki2501

sudoku

Jul 5th, 2020
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <windows.h>
  3. using namespace std;
  4.  
  5. #define ii pair<int,int>
  6. #define fi first
  7. #define sc second
  8.  
  9. #define map candidate
  10. #define cnt count
  11.  
  12. int sol[9][9];
  13. set<int> map[9][9];
  14. vector<int> tab[3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
  15.  
  16. int main() {
  17.   freopen("in" , "r", stdin );
  18.   HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  19.   SetConsoleTextAttribute(hConsole, 12 + 224);
  20.   cout << "       imilinh emulator .      " << "\n\n";
  21.   SetConsoleTextAttribute(hConsole, 15);
  22.   for (int i = 0; i < 9; i++)
  23.   for (int j = 0; j < 9; j++) {
  24.     cin >> sol[i][j];
  25.     if (sol[i][j]) map[i][j].insert(sol[i][j]);
  26.     else map[i][j] = set<int>({1, 2, 3, 4, 5, 6, 7, 8, 9});
  27.   }
  28.   int loop = 100;
  29.   while (loop --> 0) {
  30.     for (int i = 0; i < 9; i++)
  31.     for (int j = 0; j < 9; j++) {
  32.       if (sol[i][j]) {
  33.         for (int k = 0; k < 9; k++) {
  34.           if (!sol[i][k]) map[i][k].erase(sol[i][j]);
  35.           if (!sol[k][j]) map[k][j].erase(sol[i][j]);
  36.         }
  37.         for (auto k : tab[i / 3])
  38.         for (auto l : tab[j / 3]) {
  39.           if (!sol[k][l]) map[k][l].erase(sol[i][j]);
  40.         }
  41.       }
  42.     }
  43.     // ----- naked single -----
  44.     for (int i = 0; i < 9; i++)
  45.     for (int j = 0; j < 9; j++) {
  46.       if (map[i][j].size() == 1) sol[i][j] = *map[i][j].begin();
  47.     }
  48.     // ------------------------
  49.     // ----- hidden single ----
  50.     vector<multiset<int>> row(9), col(9);
  51.     vector<vector<multiset<int>>> stb(3, vector<multiset<int>>(3));
  52.     for (int i = 0; i < 9; i++)
  53.     for (int j = 0; j < 9; j++) {
  54.       for (auto k : map[i][j]) {
  55.         row[i].insert(k);
  56.         col[j].insert(k);
  57.         stb[i / 3][j / 3].insert(k);
  58.       }
  59.     }
  60.     for (int i = 0; i < 9; i++)
  61.     for (int j = 0; j < 9; j++) {
  62.       for (int k = 1; k <= 9; k++) {
  63.         int t = row[i].cnt(k) * col[j].cnt(k) * stb[i / 3][j / 3].cnt(k);
  64.         if (map[i][j].cnt(k) && t == 1) {
  65.           sol[i][j] = k;
  66.           map[i][j].clear();
  67.           map[i][j].insert(k);
  68.           break;
  69.         }
  70.       }
  71.     }
  72.     // ------------------------
  73.     // ----- pointing ---------
  74.     for (int k = 1; k <= 9; k++) {
  75.       vector<vector<vector<ii>>> ptb(3, vector<vector<ii>>(3));
  76.       for (int i = 0; i < 9; i++)
  77.       for (int j = 0; j < 9; j++) {
  78.         if (map[i][j].cnt(k)) ptb[i / 3][j / 3].push_back(ii(i, j));
  79.       }
  80.       for (int i = 0; i < 3; i++)
  81.       for (int j = 0; j < 3; j++) {
  82.         if (ptb[i][j].size()) {
  83.           int r = ptb[i][j].begin()->fi;
  84.           int c = ptb[i][j].begin()->sc;
  85.           bool br = 1, bc = 1;
  86.           for (auto t : ptb[i][j]) {
  87.             if (t.fi != r) br = 0;
  88.             if (t.sc != c) bc = 0;
  89.           }
  90.           for (int l = 0; l < 9; l++) {
  91.             if (br && l / 3 != j) map[r][l].erase(k);
  92.             if (bc && l / 3 != i) map[l][c].erase(k);
  93.           }
  94.         }
  95.       }
  96.     }
  97.     // ------------------------
  98.     // ----- claiming ---------
  99.     for (int k = 1; k <= 9; k++) {
  100.       vector<vector<int>> rw(9), cl(9);
  101.       for (int i = 0; i < 9; i++)
  102.       for (int j = 0; j < 9; j++) {
  103.         if (map[i][j].cnt(k)) {
  104.           rw[i].push_back(j);
  105.           cl[j].push_back(i);
  106.         }
  107.       }
  108.       for (int l = 0; l < 9; l++) {
  109.         if (rw[l].size()) {
  110.           int c = rw[l].front();
  111.           bool br = 1;
  112.           for (auto t : rw[l]) if (t / 3 != c / 3) br = 0;
  113.           if (br) {
  114.             for (auto h : tab[l / 3])
  115.             for (auto g : tab[c / 3]) {
  116.               if (h != l) map[h][g].erase(k);
  117.             }
  118.           }
  119.         }
  120.         if (cl[l].size()) {
  121.           int r = cl[l].front();
  122.           bool bc = 1;
  123.           for (auto t : cl[l]) if (t / 3 != r / 3) bc = 0;
  124.           if (bc) {
  125.             for (auto h : tab[r / 3])
  126.             for (auto g : tab[l / 3]) {
  127.               if (g != l) map[h][g].erase(k);
  128.             }
  129.           }
  130.         }
  131.       }
  132.     }
  133.     // ------------------------
  134.     // ----- naked pair -------
  135.     for (int i = 0; i < 9; i++)
  136.     for (int j = 0; j < 9; j++) {
  137.       if (map[i][j].size() == 2) {
  138.         for (int k = 0; k < 9; k++) {
  139.           if (k != j && map[i][j] == map[i][k]) {
  140.             for (int l = 0; l < 9; l++) {
  141.               if (l != j && l != k) {
  142.                 for (auto t : map[i][j]) map[i][l].erase(t);
  143.               }
  144.             }
  145.           }
  146.           if (k != i && map[i][j] == map[k][j]) {
  147.             for (int l = 0; l < 9; l++) {
  148.               if (l != i && l != k) {
  149.                 for (auto t : map[i][j]) map[l][j].erase(t);
  150.               }
  151.             }
  152.           }
  153.         }
  154.         for (auto k : tab[i / 3])
  155.         for (auto l : tab[j / 3]) {
  156.           if (k != i && j != l && map[i][j] == map[k][l]) {
  157.             for (auto h : tab[i / 3])
  158.             for (auto g : tab[j / 3]) {
  159.               if (!((h == i && g == j) || (h == k && g == l))) {
  160.                 for (auto t : map[i][j]) map[h][g].erase(t);
  161.               }
  162.             }
  163.           }
  164.         }
  165.       }
  166.     }
  167.     // ------------------------
  168.     // ----- hidden pair ------
  169.     for (int k = 1; k <= 9 - 1; k++)
  170.     for (int l = k + 1; l <= 9; l++) {
  171.       vector<vector<int>> ro(9), co(9);
  172.       vector<vector<vector<ii>>> ta(3, vector<vector<ii>>(3));
  173.       vector<bool> fro(9, 1), fco(9, 1);
  174.       vector<vector<bool>> fta(3, vector<bool>(3, 1));
  175.       for (int i = 0; i < 9; i++)
  176.       for (int j = 0; j < 9; j++) {
  177.         if (map[i][j].cnt(k) && map[i][j].cnt(l)) {
  178.           ro[i].push_back(j);
  179.           co[j].push_back(i);
  180.           ta[i / 3][j / 3].push_back(ii(i, j));
  181.         } else {
  182.           if (map[i][j].cnt(k) || map[i][j].cnt(l)) {
  183.             fro[i] = 0;
  184.             fco[j] = 0;
  185.             fta[i / 3][j / 3] = 0;
  186.           }
  187.         }
  188.       }
  189.       for (int i = 0; i < 9; i++) {
  190.         if (ro[i].size() == 2 && fro[i]) {
  191.           for (auto j : ro[i]) {
  192.             for (int t = 1; t <= 9; t++) {
  193.               if (t != k && t != l) map[i][j].erase(t);
  194.             }
  195.           }
  196.         }
  197.       }
  198.       for (int j = 0; j < 9; j++) {
  199.         if (co[j].size() == 2 && fco[j]) {
  200.           for (auto i : co[j]) {
  201.             for (int t = 1; t <= 9; t++) {
  202.               if (t != k && t != l) map[i][j].erase(t);
  203.             }
  204.           }
  205.         }
  206.       }
  207.       for (int h = 0; h < 3; h++)
  208.       for (int g = 0; g < 3; g++) {
  209.         if (ta[h][g].size() == 2 && fta[h][g]) {
  210.           for (auto p : ta[h][g]) {
  211.             for (int t = 1; t <= 9; t++) {
  212.               if (t != k && t != l) map[p.fi][p.sc].erase(t);
  213.             }
  214.           }
  215.         }
  216.       }
  217.     }
  218.     // ------------------------
  219.   }
  220.   for (int i = -1; i < 9; i++) {
  221.     if (i != -1) {
  222.       cout << "| ";
  223.       for (int j = 0; j < 9; j++) {
  224.         int colour = 0;
  225.         switch (sol[i][j]) {
  226.           case 1: colour = 10; break;
  227.           case 2: colour = 14; break;
  228.           case 3: colour =  4; break;
  229.           case 4: colour = 12; break;
  230.           case 5: colour =  6; break;
  231.           case 6: colour =  1; break;
  232.           case 7: colour =  5; break;
  233.           case 8: colour =  8; break;
  234.           case 9: colour =  9; break;
  235.         }
  236.         SetConsoleTextAttribute(hConsole, colour);
  237.         if (!sol[i][j]) cout << ' ';
  238.         else cout << sol[i][j];
  239.         SetConsoleTextAttribute(hConsole, 15);
  240.         if (j == 8) cout << " |\n";
  241.         else if ((j + 1) % 3 == 0) cout << " | "; else cout << "  ";
  242.       }
  243.     }
  244.     if ((i + 1) % 3 == 0) {
  245.       for (int j = 0; j <= 30; j++) {
  246.         if (j % 10 == 0) cout << '+'; else cout << '-';
  247.       }
  248.       cout << '\n';
  249.     }
  250.   }
  251.   cout << '\n';
  252.   return 0;
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement