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 83a96c6..f72b626 100644 --- a/kangrae-jo/README.md +++ b/kangrae-jo/README.md @@ -31,3 +31,4 @@ | 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)| | 29차시 | 2024.06.19 | TRIE | [[3차] 자동완성](https://school.programmers.co.kr/learn/courses/30/lessons/17685)|[#114](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/114)| +| 30차시 | 2024.07.18 | BFS | [치즈](https://www.acmicpc.net/problem/2638)|[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/117)| \ No newline at end of file