Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions kangrae-jo/Queue/30-kangrae-jo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <iostream>
#include <queue>
#include <vector>

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<vector<int>> board;

bool isIn(int y, int x) { return 0 <= y && y < N && 0 <= x && x < M; }

void markAir() {
vector<vector<bool>> visited(N, vector<bool>(M, false));
queue<pair<int, int>> 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<pair<int, int>> 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<int>(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;
}
1 change: 1 addition & 0 deletions kangrae-jo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)|