#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LOG = 31;
const ll MOD = 1000000007;
const ll inf = 1e17;
const ll B = 2305843009213693951;
#define db(x) cerr << #x << " = " << x << " | "
#define dbg(x) cerr << #x << " = " << x << "\n"
#define Algerian ios::sync_with_stdio(0);
#define OI cin.tie(NULL);
int main() {
Algerian OI
ll n, m;
cin >> n >> m;
vector<string> grid(n);
for (ll i = 0; i < n; i++) {
cin >> grid[i];
}
deque<pll> dq;
dq.push_back({0, 0});
vector<vector<ll>> dist(n, vector<ll>(m, -1));
dist[0][0] = 1;
ll res = 1;
while (dq.size()) {
auto [x, y] = dq.front();
dq.pop_front();
// if (dist[x][y] > res) { db(x); dbg(y); }
res = max(res, dist[x][y]);
vector<pll> adj = {{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}};
for (auto [cx, cy] : adj) {
if (cx < 0 || cx >= n || cy < 0 || cy >= m) continue;
if (dist[cx][cy] != -1 || grid[cx][cy] == '.') continue;
if (grid[cx][cy] == grid[x][y]) {
dist[cx][cy] = dist[x][y];
dq.push_front({cx, cy});
} else {
dist[cx][cy] = dist[x][y] + 1;
dq.push_back({cx, cy});
}
}
}
cout << res << "\n";
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |