This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define sz(v) (ll)(v.size())
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
const ll sz = 4005;
ll lv[sz][sz], vis[sz][sz];
ll dx[] = {1, 0, 0, -1};
ll dy[] = {0, 1, -1, 0};
void solve()
{
ll h, w, i, j;
cin >> h >> w;
char grid[h+5][w+5];
for(i = 1; i <= h; i++)
{
for(j = 1; j <= w; j++)
cin >> grid[i][j];
}
deque<pll>dq;
ll stx = -1, sty = -1;
for(i = 1; i <= h; i++)
{
for(j = 1; j <= w; j++)
{
if(grid[i][j] != '.')
{
stx = i;
sty = j;
break;
}
}
}
lv[stx][sty] = 1;
dq.pb({stx, sty});
while(!dq.empty())
{
ll x = dq.front().f, y = dq.front().s;
vis[x][y] = 1;
dq.pop_front();
for(i = 0; i < 4; i++)
{
ll nx = x + dx[i], ny = y + dy[i];
if(1 <= nx && nx <= h && 1 <= ny && ny <= w && !vis[nx][ny] && grid[nx][ny] != '.')
{
if(grid[nx][ny] == grid[x][y])
{
lv[nx][ny] = lv[x][y];
dq.push_front({nx, ny});
}
else
{
lv[nx][ny] = lv[x][y] + 1;
dq.pb({nx, ny});
}
}
}
}
ll res = 0;
for(i = 1; i <= h; i++)
{
for(j = 1; j <= w; j++)
{
if(grid[i][j] != '.')
res = max(res, lv[i][j]);
}
}
cout << res << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tests = 1;
//cin >> tests;
while(tests--)
{
solve();
}
}
/*
5 5
1 2 3 4 5
2 3 5 4
3 3 5
1 2 5
2 1 3 3
1 1 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |