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 f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
using namespace std;
int d[4005][4005];
char c[4005][4005];
bool used[4005][4005];
int main(){
int n, m; cin >> n >> m;
for(int i = 0; i <= n + 1; i++){
for(int j= 0; j <= m + 1; j++) c[i][j] = '.';
}
for(int i= 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
d[i][j] = 1e9;
}
}
deque <pii> q;
q.push_front({1, 1});
d[1][1] = 1;
int ans = 0;
while(!q.empty()){
pii v = q.front(); q.pop_front();
if(used[v.f][v.s] == true) continue;
ans = max(ans, d[v.f][v.s]);
used[v.f][v.s] = true;
int x = v.f, y = v.s;
if(c[x - 1][y] != '.'){
int len = (c[x - 1][y] != c[x][y]);
d[x - 1][y] = min(d[x - 1][y], d[x][y] + len);
if(len) q.pb({x - 1, y});
else q.push_front({x - 1, y});
}
if(c[x + 1][y] != '.'){
int len = (c[x + 1][y] != c[x][y]);
d[x + 1][y] = min(d[x + 1][y], d[x][y] + len);
if(len) q.pb({x + 1, y});
else q.push_front({x + 1, y});
}
if(c[x][y - 1] != '.'){
int len = (c[x][y - 1] != c[x][y]);
d[x][y - 1] = min(d[x][y - 1], d[x][y] + len);
if(len) q.pb({x, y -1});
else q.push_front({x, y - 1});
}
if(c[x][y + 1] != '.'){
int len = (c[x][y + 1] != c[x][y]);
d[x][y + 1] = min(d[x][y + 1], d[x][y] + len);
if(len) q.pb({x, y +1});
else q.push_front({x, y + 1});
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |