#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define solveT() {int t; cin >> t; while (t--) solve();};
#define int long long
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void solve() {
int h, w;
cin >> h >> w;
vector<string> arr(h);
for (int i = 0; i < h; i++) {
cin >> arr[i];
}
vector<pair<int, int>> dr = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
vector<vector<bool>> vis(h, vector<bool>(w, false));
vector<vector<int>> depth(h, vector<int>(w, -1));
auto bfs = [&](int i, int j) {
deque<pair<int, int>> q;
q.emplace_front(i, j);
depth[i][j] = 1;
int ans{0};
while (!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
ans = max(ans, depth[x][y]);
for (auto [dx, dy] : dr) {
if (x + dx >= 0 && x + dx < h && y + dy >= 0 && y + dy < w && arr[x + dx][y + dy] != '.' && !vis[x + dx][y + dy]) {
vis[x + dx][y + dy] = true;
if (arr[x][y] == arr[x + dx][y + dy]) {
q.emplace_front(x + dx, y + dy);
depth[x + dx][y + dy] = depth[x][y];
}
else {
q.emplace_back(x + dx, y + dy);
depth[x + dx][y + dy] = 1 + depth[x][y];
}
}
}
}
return ans;
};
cout << bfs(0, 0) << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("file.in", "r", stdin);
//freopen("file.out", "w", stdout);
solve();
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |