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 ("Ofast")
//#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 + 5;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int n, m, indtmp = 0, indnxt = 0;
bool mark[maxn5][maxn5];
pair<int, int> nxt[maxn5 * maxn5 * 4], tmp[maxn5 * maxn5 * 4];
string s[maxn5];
void dfs(int x, int y){
mark[x][y] = true;
if(x && !mark[x - 1][y] && s[x - 1][y] != '.'){
if(s[x - 1][y] == s[x][y])
dfs(x - 1, y);
else
nxt[indnxt++] = {x - 1, y};
}
if(x + 1 < n && !mark[x + 1][y] && s[x + 1][y] != '.'){
if(s[x + 1][y] == s[x][y])
dfs(x + 1, y);
else
nxt[indnxt++] = {x + 1, y};
}
if(y && !mark[x][y - 1] && s[x][y - 1] != '.'){
if(s[x][y - 1] == s[x][y])
dfs(x, y - 1);
else
nxt[indnxt++] = {x, y - 1};
}
if(y + 1 < m && !mark[x][y + 1] && s[x][y + 1] != '.'){
if(s[x][y + 1] == s[x][y])
dfs(x, y + 1);
else
nxt[indnxt++] = {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];
dfs(0, 0);
int ans = 1;
while(indnxt){
ans++;
indtmp = indnxt;
for(int i = 0; i < indnxt; i++)
tmp[i] = nxt[i];
indnxt = 0;
for(int i = 0; i < indtmp; i++) if(!mark[tmp[i].fi][tmp[i].se])
dfs(tmp[i].fi, tmp[i].se);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |