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>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int n, m;
inline bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
vector<string> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
deque<pair<int, int>> q;
q.push_front({0, 0});
bool visited[n + 2][m + 2];
int dist[n + 2][m + 2];
memset(visited, 0, sizeof(visited));
memset(dist, 0, sizeof(dist));
visited[0][0] = 1;
dist[0][0] = 1;
int ans = 1;
while (q.size() > 0) {
auto [x, y] = q.front();
q.pop_front();
ans = max(ans, dist[x][y]);
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (valid(nx, ny) && !visited[nx][ny] && v[nx][ny] != '.') {
visited[nx][ny] = 1;
dist[nx][ny] = dist[x][y] + (v[nx][ny] != v[x][y]);
if (v[nx][ny] != v[x][y]) q.push_back({nx, ny});
else q.push_front({nx, ny});
}
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |