This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define MAXN 4010
using namespace std;
char c[MAXN][MAXN];
ll a[MAXN][MAXN],cur=1,dist[MAXN*MAXN];
bool V[MAXN][MAXN];
set<pair<ll,ll> > s;
vector<ll> G[MAXN*MAXN];
void DFS(ll i,ll j)
{
a[i][j]=cur;
V[i][j]=true;
for (ll x=-1;x<2;x++)
{
for (ll y=-1;y<2;y++)
{
if (abs(x)+abs(y)==1)
{
if (c[i+x][j+y]==c[i][j] && !V[i+x][j+y])
DFS(i+x,j+y);
else if (c[i+x][j+y]!=c[i][j] && V[i+x][j+y] && !s.count({a[i+x][j+y],cur}))
{
G[a[i+x][j+y]].push_back(cur);
G[cur].push_back(a[i+x][j+y]);
s.insert({a[i+x][j+y],cur});
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll h,w;
cin >> h >> w;
for (ll i=1;i<=h;i++)
{
for (ll j=1;j<=w;j++)
cin >> c[i][j];
}
for (ll i=1;i<=h;i++)
{
for (ll j=1;j<=w;j++)
{
if (!V[i][j] && c[i][j]!='.')
{
DFS(i,j);
cur++;
}
}
}
queue<ll> q;
q.push(1);
dist[1]=1;
ll ans=0;
while (!q.empty())
{
ll i=q.front();
q.pop();
ans=max(ans,dist[i]);
for (ll next : G[i])
{
if (!dist[next])
{
dist[next]=dist[i]+1;
q.push(next);
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |