Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define prm(x) x.begin(), x.end()
- #define srt(x) sort(prm(x))
- #define rvs(x) reverse(prm(x))
- using namespace std;
- using ll = long long;
- using ld = long double;
- vector<ll> p1{1, -1, 0, 0}, p2{0, 0, 1, -1};
- bool chk(vector<vector<ll>> &v)
- {
- ll a = v.at(0).at(0);
- for (auto &c1 : v)
- {
- for (auto &c2 : c1)
- {
- if (c2 != a)
- {
- return true;
- }
- }
- }
- return false;
- }
- void rep(vector<vector<ll>> &v, ll ch)
- {
- ll num = v.at(0).at(0);
- vector<vector<bool>> ex(v.size(), vector<bool>(v.size(), false));
- deque<pair<ll, ll>> bfs{{0, 0}};
- //***************************************************************** BFS ↓
- while (!bfs.empty())
- {
- ex.at(bfs.at(0).first).at(bfs.at(0).second) = true;
- v.at(bfs.at(0).first).at(bfs.at(0).second) = ch;
- for (int i = 0; i < 4; i++)
- {
- ll x = bfs.at(0).first + p1.at(i);
- ll y = bfs.at(0).second + p2.at(i);
- if (x > -1 && x < v.size() && y > -1 && y < v.size())
- {
- if (!ex.at(x).at(y))
- {
- if (v.at(x).at(y) == num)
- {
- bfs.push_back({x, y});
- }
- }
- }
- }
- bfs.pop_front();
- }
- //***************************************************************** BFS ↑
- }
- pair<ll, ll> vrf(vector<vector<ll>> &v)
- {
- ll mx = 0, tc = 0;
- ll num = v.at(0).at(0);
- vector<vector<bool>> ex(v.size(), vector<bool>(v.size(), false));
- vector<ll> cal(7);
- deque<pair<ll, ll>> bfs{{0, 0}};
- ex.at(0).at(0) = true;
- //***************************************************************** BFS ↓
- while (!bfs.empty())
- {
- for (int i = 0; i < 4; i++)
- {
- ll c1 = bfs.at(0).first;
- ll c2 = bfs.at(0).second;
- ll x = bfs.at(0).first + p1.at(i);
- ll y = bfs.at(0).second + p2.at(i);
- if (x > -1 && x < v.size() && y > -1 && y < v.size())
- {
- if (ex.at(x).at(y) == false)
- {
- if (v.at(c1).at(c2) == num)
- {
- if (v.at(x).at(y) != num)
- {
- cal.at(v.at(x).at(y)) += 1;
- }
- ex.at(x).at(y) = true;
- bfs.push_back({x, y});
- }
- else
- {
- if (v.at(c1).at(c2) == v.at(x).at(y))
- {
- cal.at(v.at(x).at(y)) += 1;
- ex.at(x).at(y) = true;
- bfs.push_back({x, y});
- }
- }
- }
- }
- }
- bfs.pop_front();
- }
- //***************************************************************** BFS ↑
- for (int i = 1; i < 7; i++)
- {
- if (cal.at(i) > mx)
- {
- mx = cal.at(i);
- tc = i;
- }
- }
- return {mx, tc};
- }
- int main()
- {
- ll t;
- cin >> t;
- while (t-- > 0)
- {
- ll n;
- cin >> n;
- vector<vector<ll>> v(n, vector<ll>(n));
- //---------------------------------- Read
- for (auto &c1 : v)
- {
- for (auto &c2 : c1)
- {
- char x;
- cin >> x;
- c2 = x - '0';
- }
- }
- //---------------------------------- Read
- //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Solving
- vector<ll> op(7);
- if (chk(v))
- {
- while (true)
- {
- ll mx = 0, ch = 0;
- pair<ll, ll> ax;
- ax = vrf(v);
- mx = ax.first;
- ch = ax.second;
- if (ch == 0)
- {
- break;
- }
- op.at(ch) += 1;
- rep(v, ch);
- }
- }
- //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Solving
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Output
- ll sum = 0;
- for (auto cl : op)
- {
- sum += cl;
- }
- cout << sum << endl;
- for (int i = 1; i < 7; i++)
- {
- cout << op.at(i) << " ";
- }
- cout << endl;
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Output
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment