#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define tii tuple<int, int, int>
#define f first
#define s second
#define em emplace
#define emb emplace_back
#define ve vector
using namespace std;
const int inf = 1e18;
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
ve<ve<char>> a(n, ve<char> (m));
ve<ve<int>> dis(n, ve<int> (m, inf));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
if (a[0][0] == '.') { cout << 0; return 0; }
deque<pii> q;
q.emb(0, 0);
dis[0][0] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] != a[x][y] || dis[nx][ny] <= dis[x][y]) continue;
dis[nx][ny] = dis[x][y];
q.emplace_front(nx, ny);
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '.' || a[nx][ny] == a[x][y] || dis[nx][ny] <= dis[x][y] + 1) continue;
dis[nx][ny] = dis[x][y] + 1;
q.emb(nx, ny);
}
}
int mx = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '.') continue;
mx = max(mx, dis[i][j]);
}
}
cout << mx;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |