#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
bool inBounds(int x, int y, int r, int c) {
return x >= 0 && x < r && y >= 0 && y < c;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<vector<char>> grid(h, vector<char>(w));
vector<vector<int>> score(h, vector<int>(w, INT_MAX));
score.at(0).at(0) = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> grid.at(i).at(j);
}
}
queue<pii> q;
q.push({0, 0});
while (!q.empty()) {
pii p = q.front();
q.pop();
int x = p.first, y = p.second;
for (int k = -2; k < 2; k++) {
int dx = k % 2, dy = (k + 1) % 2;
if (inBounds(x + dx, y + dy, h, w) && score.at(x).at(y) + (grid.at(x).at(y) != grid.at(x + dx).at(y + dy)) < score.at(x + dx).at(y + dy) && !(x + dx == h - 1 && y + dy == w - 1)) {
score.at(x + dx).at(y + dy) = score.at(x).at(y) + (grid.at(x).at(y) != grid.at(x + dx).at(y + dy));
q.push({x + dx, y + dy});
}
}
}
int ans = 0;
for (int i = 0; i < h; i++) for (int j = 0; j < (i + 1 == h ? w - 1 : w); j++) ans = max(ans, score.at(i).at(j));
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |