이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
const int MAXN = 4000;
int n, m;
char a[MAXN][MAXN];
bool check (int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '.';
}
int dist[MAXN][MAXN];
deque <pair <int, int>> cur;
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j];
cur.push_back({0, 0});
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) dist[i][j] = 1e9;
dist[0][0] = 1;
int ans = 1;
while (!cur.empty()) {
auto k = cur.front(); cur.pop_front();
ans = max(ans, dist[k.first][k.second]);
for (int i = 0; i < 4; i++) {
int x = k.first + dx[i], y = k.second + dy[i];
if (!check(x, y)) continue;
if (a[x][y] == a[k.first][k.second]) {
if (dist[k.first][k.second] < dist[x][y]) {
dist[x][y] = dist[k.first][k.second];
cur.push_front({x, y});
}
} else {
if (dist[k.first][k.second] + 1 < dist[x][y]) {
dist[x][y] = dist[k.first][k.second] + 1;
cur.push_back({x, y});
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |