This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~ Be Name Khoda ~
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 4e3 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int n, m, cnt = 0;
bool mark[maxn5][maxn5];
vector <pair<int, int>> nxt, tmp;
pair<int, int> q[maxn5 * maxn5];
string s[maxn5];
void bfs(int xx, int yy){
int l = 0, r = 1;
q[0] = {xx, yy};
mark[xx][yy] = true;
while(l < r){
int x = q[l].fi, y = q[l].se;
l++;
if(x && !mark[x - 1][y] && s[x - 1][y] != '.'){
if(s[x - 1][y] == s[x][y]){
mark[x - 1][y] = true;
q[r++] = {x - 1, y};
}
else
nxt.pb({x - 1, y});
}
if(x + 1 < n && !mark[x + 1][y] && s[x + 1][y] != '.'){
if(s[x + 1][y] == s[x][y]){
mark[x + 1][y] = true;
q[r++] = {x + 1, y};
}
else
nxt.pb({x + 1, y});
}
if(y && !mark[x][y - 1] && s[x][y - 1] != '.'){
if(s[x][y - 1] == s[x][y]){
mark[x][y - 1] = true;
q[r++] = {x, y - 1};
}
else
nxt.pb({x, y - 1});
}
if(y + 1 < m && !mark[x][y + 1] && s[x][y + 1] != '.'){
if(s[x][y + 1] == s[x][y]){
mark[x][y + 1] = true;
q[r++] = {x, y + 1};
}
else
nxt.pb({x, y + 1});
}
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> s[i];
bfs(0, 0);
int ans = 1;
while(nxt.size()){
ans++;
tmp.clear();
for(auto u : nxt)
tmp.pb(u);
nxt.clear();
for(auto u : tmp) if(!mark[u.fi][u.se])
bfs(u.fi, u.se);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |