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 vl vector<ll>
#define sl set<ll>
#define all(v) v.begin(), v.end()
#define pb push_back
#define s second
#define f first
#define pll pair<ll, ll>
#define pii pair<int, int>
using namespace std;
int n, m, i, j, depth[4001][4001];
int neighborx[4] = {-1, 1, 0, 0};
int neighbory[4] = {0, 0, 1, -1};
string a[4001];
bool isvalid(int c, int d)
{
if(c < 0 || d < 0 || c >= n || d >= m || a[c][d] == '.') return false;
return true;
}
int main()
{
cin >> n >> m;
for(i = 0; i < n; i++) cin >> a[i];
depth[0][0] = 1;
deque<pair<int, int>>q;
q.push_back({0, 0});
int ans = 0;
while(!q.empty())
{
auto s = q.front();
q.pop_front();
int x = s.f, y = s.s;
ans = max(ans, depth[x][y]);
for(i = 0; i < 4; i++)
{
int nx = x + neighborx[i], ny = y + neighbory[i];
if(isvalid(nx, ny) && depth[nx][ny] == 0)
{
if(a[x][y] == a[nx][ny])
{
depth[nx][ny] = depth[x][y];
q.push_front({nx, ny});
}
else
{
depth[nx][ny] = 1 + depth[x][y];
q.push_back({nx, ny});
}
}
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |