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;
const int N = 4001;
const int M = 4001;
int n, m;
char a[N][M];
int d[N][M];
int ans = 1;
bool visited[N][M];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
bool inside(int r, int c) {
if (r < 0 || r > n || c < 0 || c > m) return false;
if (a[r][c] != '.') return false;
return true;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0 ; i < n; i++){
string s; cin >> s;
for(int j = 0 ; j < m; j++){
a[i][j] = s[j];
}
}
deque<pair<int, int>> q;
visited[0][0] = true;
q.push_back({0, 0});
while(!q.empty()){
auto node = q.front();
q.pop_front();
ans = max(ans, d[node.first][node.second]);
for(int i = 0 ; i < 4; i++){
int x = node.first + dx[i]; int y = node.second + dy[i];
if(inside(x, y) && !visited[x][y]){
if(a[node.first][node.second] == a[x][y]){
d[x][y] = d[node.first][node.second];
q.push_front({x, y});
visited[x][y] = true;
}
else {
d[x][y] = d[node.first][node.second] + 1;
q.push_back({x, y});
visited[x][y] = true;
}
}
}
}
cout << ans + 1 << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |