이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define nl '\n'
#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
std::vector<std::string> s(N);
for (int i = 0; i < N; i++) {
std::cin >> s[i];
}
std::vector vis(N, std::vector<int>(M));
auto nqg = vis;
const std::vector<std::pair<int, int>> dxy = {
{0, -1},
{0, 1},
{-1, 0},
{1, 0}
};
auto valid = [&](int x, int y) {
return 0 <= x && x < N && 0 <= y && y < M;
};
std::queue<std::pair<int, int>> q;
q.push({0, 0});
vis[0][0] = 1;
int cnt =0 ;
while (q.size()) {
dbg(q);
cnt++;
std::vector<std::pair<int, int>> nq;
while (q.size()) {
auto [x, y] = q.front();
q.pop();
for (auto [dx, dy] : dxy) {
int nx = x + dx, ny = y + dy;
if (!valid(nx, ny)) continue;
if (vis[nx][ny]) continue;
if (s[nx][ny] == '.') continue;
if (s[nx][ny] == s[x][y]) {
q.emplace(nx, ny);
vis[nx][ny] = 1;
} else {
if (nqg[nx][ny]) continue;
nq.emplace_back(nx, ny);
nqg[nx][ny] = 1;
}
}
}
std::queue<std::pair<int, int>> gg;
for (auto [x, y] : nq) {
vis[x][y] = 1;
gg.emplace(x, y);
}
std::swap(gg, q);
}
std::cout << cnt << nl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |