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 pair<int, int> pii;
const int MAX = 1015;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << ": " << "[ " << x << " ]\n"
int n, m, ans, d[MAX][MAX];
char t[MAX][MAX];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
memset(t, '*', sizeof(t));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> t[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
d[i][j] = INF;
deque<pair<int, pii>> q;
q.push_front({1, {1, 1}});
d[1][1] = 1;
while(q.size()){
int x = q.front().sc.fr, y = q.front().sc.sc;
q.pop_front();
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(t[nx][ny] == '*') continue;
int w = (t[x][y] == t[nx][ny] ? 0 : 1);
if(d[nx][ny] > d[x][y] + w){
d[nx][ny] = d[x][y] + w;
if(w == 0) q.push_front({d[nx][ny], {nx, ny}});
else q.push_back({d[nx][ny], {nx, ny}});
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(d[i][j] == INF) continue;
ans = max(ans, d[i][j]);
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |