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>
using namespace std;
typedef long long ll;
typedef vector <ll> vi;
typedef pair <ll,ll> pi;
#define f first
#define s second
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
string grid[4000];
int depth[4000][4000], ans = 1;
int n, m;
bool inside(int x, int y){
return x >= 0 && y >= 0 && x < n && y < m && grid[x][y] != '.';
}
int main() {
//ios::sync_with_stdio(0); cin.tie(0);
//freopen("guard.in", "r", stdin);
//freopen("guard.out", "w", stdout);
cin >> n >> m;
for(int i=0; i<n; i++){
cin >> grid[i];
}
deque <pi> q;
q.push_back({0, 0});
depth[0][0] = 1;
while(q.size()){
int u = q.front().f, v = q.front().s;
ans = max(ans, depth[u][v]);
q.pop_front();
for(int i=0; i<4; i++){
int x = u + dx[i], y = v + dy[i];
if(!inside(x, y) || depth[x][y] != 0) continue;
if(grid[u][v] == grid[x][y]){
depth[x][y] = depth[u][v];
q.push_front({x, y});
}else{
depth[x][y] = depth[u][v] + 1;
q.push_back({x, y});
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |