이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <queue>
int main() {
int N, M;
std::cin >> N >> M;
char g[N * M];
for (int i = 0; i < N * M; i++) std::cin >> g[i];
int D[4] {-1, -M, 1, M};
int d[N * M];
std::fill(d, d + N * M, -1);
std::queue<int> q, pq;
q.push(0);
d[0] = 1;
while (!q.empty() || !pq.empty()) {
int p;
if (!pq.empty()) p = pq.front(), pq.pop();
else p = q.front(), q.pop();
for (int i : D) {
if (p + i >= 0 && p + i < N * M && !(i == -1 && p % M == 0) && !(i == 1 && p % M == M - 1)) {
if (g[p + i] != '.' && d[p + i] == -1) {
if (g[p] == g[p + i]) pq.push(p + i), d[p + i] = d[p];
else q.push(p + i), d[p + i] = d[p] + 1;
}
}
}
}
int w = 0;
for (int i = 0; i < N * M; i++) if (d[i] > w) w = d[i];
std::cout << w << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |