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