egogoboy

домино

Jan 9th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include<algorithm>
  5. #include<map>
  6. #include<string>
  7. #include<cmath>
  8. #define all(container) container.begin(), container.end()
  9. #define fors(counter, start, finish) for (ll counter = start; counter < finish; ++counter)
  10. #define forb(counter, start, finish) for (int counter = start; counter >= finish; --counter)
  11. #define vec(type) std::vector<type>
  12. #define dvec(type) std::vector<std::vector<type>>
  13. #define cout(val) std::cout << val << std::endl;
  14. #define ll int64_t
  15. //#define fin std::cin
  16.  
  17. ll size, w, h;
  18. int ans = 1000;
  19. std::map<char, std::pair<std::pair<int, int>, std::pair<int, int>>> lit;
  20.  
  21. std::map<char, bool> done;
  22. vec(int) di = { 0, 0, -1, 1 };
  23. vec(int) dj = { -1, 1, 0, 0 };
  24.  
  25. int check(dvec(bool)& pol) {
  26.     int res = 0;
  27.     fors(i, 0, w) {
  28.         fors(j, 0, h) {
  29.             fors(q, 0, 4) {
  30.                 i += di[q]; j += dj[q];
  31.                 if (i >= 0 && j >= 0 && i < w && j < h) {
  32.                     if (pol[i - di[q]][j - dj[q]] == pol[i][j]) {
  33.                         res++;
  34.                     }
  35.                 }
  36.                 i -= di[q]; j -= dj[q];
  37.             }
  38.         }
  39.     }
  40.     return res;
  41. }
  42.  
  43. dvec(bool) rec(dvec(bool)& pol, dvec(bool)& res) {
  44.     bool f = false;
  45.     for (auto& i : lit) {
  46.         if (done[i.first]) {
  47.             f = true;
  48.             done[i.first] = false;
  49.             pol[i.second.first.first][i.second.first.second] = true;
  50.             pol[i.second.second.first][i.second.second.second] = false;
  51.             rec(pol, res);
  52.             pol[i.second.first.first][i.second.first.second] = false;
  53.             pol[i.second.second.first][i.second.second.second] = true;
  54.             rec(pol, res);
  55.             done[i.first] = true;
  56.         }
  57.     }
  58.     int temp = check(pol);
  59.     if (!f && ans > temp) {
  60.         res = pol;
  61.         ans = temp;
  62.     }
  63.     return res;
  64. }
  65.  
  66. int main() {
  67.     std::ifstream fin("input.txt");
  68.     //std::ofstream fout("output.txt");
  69.     fin >> w >> h;
  70.     size = w * h / 2;
  71.     dvec(char) dom(w, vec(char) (h));
  72.     dvec(bool) pol(w, vec(bool) (h));
  73.     dvec(bool) res(w, vec(bool) (h));
  74.     fors(i, 0, w) {
  75.         fors(j, 0, h) {
  76.             fin >> dom[i][j];
  77.             if (lit[dom[i][j]].first.first == 0) {
  78.                 lit[dom[i][j]].first.first = i + 1;
  79.                 lit[dom[i][j]].first.second = j + 1;
  80.                 done[dom[i][j]] = true;
  81.             }
  82.             else {
  83.                 lit[dom[i][j]].first.first--;
  84.                 lit[dom[i][j]].first.second--;
  85.                 lit[dom[i][j]].second.first = i;
  86.                 lit[dom[i][j]].second.second = j;
  87.                 done[dom[i][j]] = true;
  88.             }
  89.         }
  90.     }
  91.  
  92.     res = rec(pol, res);
  93.  
  94.     fors(i, 0, w) {
  95.         fors(j, 0, h)
  96.             std::cout << (res[i][j] == 0 ? '-' : '+');
  97.         std::cout << std::endl;
  98.     }
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment