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 = 4005, INF = 2e9, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int n, m, d[N][N];
char a[N][N];
inline bool check(int x, int y){
return 1 <= x && x <= n && 1 <= y && y <= m;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
d[i][j] = INF;
}
}
deque < pair < int, int > > q;
q.push_back(make_pair(1, 1));
d[1][1] = 1;
while(!q.empty()){
int x = q.front().first,
y = q.front().second;
q.pop_front();
for(int i = 0; i < 4; i++){
int xx = x + dx[i],
yy = y + dy[i];
if(check(xx, yy) && a[xx][yy] != '.'){
int val = d[x][y] + (a[x][y] != a[xx][yy]);
if(d[xx][yy] > val){
d[xx][yy] = val;
if(a[x][y] == a[xx][yy]){
q.push_front(make_pair(xx, yy));
}
else{
q.push_back(make_pair(xx, yy));
}
}
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j] != '.'){
ans = max(ans, d[i][j]);
}
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |