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 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int n, m;
bool mark[maxn5][maxn5];
vector <pair<int, int>> nxt, tmp;
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.pb({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.pb({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.pb({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.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];
dfs(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])
dfs(u.fi, u.se);
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |