#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
using pii = pair<int, int>;
bool in_bounds(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, 0));
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> grid.at(i).at(j);
vector<vector<int>> vis(h, vector<int>(w, INT_MAX));
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (grid.at(i).at(j) == '.') vis.at(i).at(j) = 0;
vis.at(0).at(0) = 1;
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 (in_bounds(x + dx, y + dy, h, w)) {
if (grid.at(x + dx).at(y + dy) == '.') continue;
if (grid.at(x).at(y) == grid.at(x + dx).at(y + dy)) {
if (vis.at(x).at(y) < vis.at(x + dx).at(y + dy)) {
vis.at(x + dx).at(y + dy) = vis.at(x).at(y);
q.push({x + dx, y + dy});
}
}
else {
if (1 + vis.at(x).at(y) < vis.at(x + dx).at(y + dy)) {
vis.at(x + dx).at(y + dy) = 1 + vis.at(x).at(y);
q.push({x + dx, y + dy});
}
}
}
}
}
int ans = 0;
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) ans = max(ans, vis.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... |