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