xT30x

Untitled

May 23rd, 2023
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define prm(x) x.begin(), x.end()
  4. #define srt(x) sort(prm(x))
  5. #define rvs(x) reverse(prm(x))
  6.  
  7. using namespace std;
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. vector<ll> p1{1, -1, 0, 0}, p2{0, 0, 1, -1};
  12. bool chk(vector<vector<ll>> &v)
  13. {
  14.     ll a = v.at(0).at(0);
  15.     for (auto &c1 : v)
  16.     {
  17.         for (auto &c2 : c1)
  18.         {
  19.             if (c2 != a)
  20.             {
  21.                 return true;
  22.             }
  23.         }
  24.     }
  25.     return false;
  26. }
  27.  
  28. void rep(vector<vector<ll>> &v, ll ch)
  29. {
  30.     ll num = v.at(0).at(0);
  31.     vector<vector<bool>> ex(v.size(), vector<bool>(v.size(), false));
  32.     deque<pair<ll, ll>> bfs{{0, 0}};
  33.     //***************************************************************** BFS ↓
  34.     while (!bfs.empty())
  35.     {
  36.         ex.at(bfs.at(0).first).at(bfs.at(0).second) = true;
  37.         v.at(bfs.at(0).first).at(bfs.at(0).second) = ch;
  38.         for (int i = 0; i < 4; i++)
  39.         {
  40.             ll x = bfs.at(0).first + p1.at(i);
  41.             ll y = bfs.at(0).second + p2.at(i);
  42.             if (x > -1 && x < v.size() && y > -1 && y < v.size())
  43.             {
  44.                 if (!ex.at(x).at(y))
  45.                 {
  46.                     if (v.at(x).at(y) == num)
  47.                     {
  48.  
  49.                         bfs.push_back({x, y});
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         bfs.pop_front();
  55.     }
  56.     //***************************************************************** BFS ↑
  57. }
  58.  
  59. pair<ll, ll> vrf(vector<vector<ll>> &v)
  60. {
  61.     ll mx = 0, tc = 0;
  62.     ll num = v.at(0).at(0);
  63.  
  64.     vector<vector<bool>> ex(v.size(), vector<bool>(v.size(), false));
  65.     vector<ll> cal(7);
  66.     deque<pair<ll, ll>> bfs{{0, 0}};
  67.  
  68.     ex.at(0).at(0) = true;
  69.     //***************************************************************** BFS ↓
  70.     while (!bfs.empty())
  71.     {
  72.  
  73.         for (int i = 0; i < 4; i++)
  74.         {
  75.             ll c1 = bfs.at(0).first;
  76.             ll c2 = bfs.at(0).second;
  77.             ll x = bfs.at(0).first + p1.at(i);
  78.             ll y = bfs.at(0).second + p2.at(i);
  79.             if (x > -1 && x < v.size() && y > -1 && y < v.size())
  80.             {
  81.                 if (ex.at(x).at(y) == false)
  82.                 {
  83.                     if (v.at(c1).at(c2) == num)
  84.                     {
  85.                         if (v.at(x).at(y) != num)
  86.                         {
  87.                             cal.at(v.at(x).at(y)) += 1;
  88.                         }
  89.                         ex.at(x).at(y) = true;
  90.                         bfs.push_back({x, y});
  91.                     }
  92.                     else
  93.                     {
  94.                         if (v.at(c1).at(c2) == v.at(x).at(y))
  95.                         {
  96.                             cal.at(v.at(x).at(y)) += 1;
  97.                             ex.at(x).at(y) = true;
  98.                             bfs.push_back({x, y});
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.         bfs.pop_front();
  105.     }
  106.     //***************************************************************** BFS ↑
  107.  
  108.     for (int i = 1; i < 7; i++)
  109.     {
  110.         if (cal.at(i) > mx)
  111.         {
  112.             mx = cal.at(i);
  113.             tc = i;
  114.         }
  115.     }
  116.     return {mx, tc};
  117. }
  118.  
  119. int main()
  120. {
  121.     ll t;
  122.     cin >> t;
  123.     while (t-- > 0)
  124.     {
  125.         ll n;
  126.         cin >> n;
  127.         vector<vector<ll>> v(n, vector<ll>(n));
  128.         //---------------------------------- Read
  129.         for (auto &c1 : v)
  130.         {
  131.             for (auto &c2 : c1)
  132.             {
  133.                 char x;
  134.                 cin >> x;
  135.                 c2 = x - '0';
  136.             }
  137.         }
  138.         //---------------------------------- Read
  139.  
  140.         //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Solving
  141.         vector<ll> op(7);
  142.         if (chk(v))
  143.         {
  144.             while (true)
  145.             {
  146.                 ll mx = 0, ch = 0;
  147.                 pair<ll, ll> ax;
  148.                 ax = vrf(v);
  149.                 mx = ax.first;
  150.                 ch = ax.second;
  151.                 if (ch == 0)
  152.                 {
  153.                     break;
  154.                 }
  155.                 op.at(ch) += 1;
  156.                 rep(v, ch);
  157.             }
  158.         }
  159.         //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Solving
  160.  
  161.         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Output
  162.         ll sum = 0;
  163.         for (auto cl : op)
  164.         {
  165.             sum += cl;
  166.         }
  167.         cout << sum << endl;
  168.         for (int i = 1; i < 7; i++)
  169.         {
  170.             cout << op.at(i) << " ";
  171.         }
  172.         cout << endl;
  173.         //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Output
  174.     }
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment