From b5ce1cbd8053b49a48f28be66f43e6e4910c69b0 Mon Sep 17 00:00:00 2001 From: kangrae Date: Fri, 18 Jul 2025 15:30:46 +0900 Subject: [PATCH] =?UTF-8?q?2025-07-18=20=EC=B9=98=EC=A6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kangrae-jo/Queue/30-kangrae-jo.cpp | 90 ++++++++++++++++++++++++++++++ kangrae-jo/README.md | 1 + 2 files changed, 91 insertions(+) create mode 100644 kangrae-jo/Queue/30-kangrae-jo.cpp diff --git a/kangrae-jo/Queue/30-kangrae-jo.cpp b/kangrae-jo/Queue/30-kangrae-jo.cpp new file mode 100644 index 0000000..162f4b5 --- /dev/null +++ b/kangrae-jo/Queue/30-kangrae-jo.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +const int AIR = -1; +const int EMPTY = 0; +const int CHEESE = 1; +const int OFFSET[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; + +int N, M; +vector> board; + +bool isIn(int y, int x) { return 0 <= y && y < N && 0 <= x && x < M; } + +void markAir() { + vector> visited(N, vector(M, false)); + queue> q; + q.push({0, 0}); + visited[0][0] = true; + board[0][0] = AIR; + + while (!q.empty()) { + auto [y, x] = q.front(); + q.pop(); + + for (int dir = 0; dir < 4; dir++) { + int y_ = y + OFFSET[dir][0]; + int x_ = x + OFFSET[dir][1]; + if (isIn(y_, x_) && !visited[y_][x_] && board[y_][x_] == EMPTY) { + visited[y_][x_] = true; + board[y_][x_] = AIR; + q.push({y_, x_}); + } + } + } +} + +bool melt() { + vector> cheeseToMelt; + for (int y = 0; y < N; y++) { + for (int x = 0; x < M; x++) { + if (board[y][x] == CHEESE) { + int contact = 0; + for (int dir = 0; dir < 4; dir++) { + int y_ = y + OFFSET[dir][0]; + int x_ = x + OFFSET[dir][1]; + if (isIn(y_, x_) && board[y_][x_] == AIR) contact++; + } + if (contact >= 2) cheeseToMelt.push_back({y, x}); + } + } + } + for (auto [y, x] : cheeseToMelt) board[y][x] = EMPTY; + + return !cheeseToMelt.empty(); +} + +void resetAir() { + for (int y = 0; y < N; y++) { + for (int x = 0; x < M; x++) { + if (board[y][x] == AIR) board[y][x] = EMPTY; + } + } +} + +int main() { + ios_base::sync_with_stdio(0); + cout.tie(0); + cin.tie(0); + + cin >> N >> M; + board.assign(N, vector(M)); + for (int y = 0; y < N; y++) { + for (int x = 0; x < M; x++) { + cin >> board[y][x]; + } + } + + int answer = 0; + while (++answer) { + markAir(); // 바깥 공기 == AIR + if (!melt()) break; // 녹일 치즈 없으면 종료 + resetAir(); // AIR = EMPTY 복구 + } + cout << answer - 1; + + return 0; +} \ No newline at end of file diff --git a/kangrae-jo/README.md b/kangrae-jo/README.md index 6f1899e..6993d96 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -30,3 +30,4 @@ | 26차시 | 2024.05.19 | DFS | [길 찾기 게임](https://school.programmers.co.kr/learn/courses/30/lessons/42892)|[#103](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/103)| | 27차시 | 2024.06.01 | DFS | [개미굴](https://www.acmicpc.net/problem/14725)|[#107](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/107)| | 28차시 | 2024.06.10 | BFS | [경주로 건설](https://school.programmers.co.kr/learn/courses/30/lessons/67259)|[#110](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/110)| +| 30차시 | 2024.07.18 | BFS | [치즈](https://www.acmicpc.net/problem/2638)|[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/117)|