[C++] BOJ 2638 - ์น์ฆProblem Solving/Baekjoon Online Judge (BOJ)2025. 4. 20. 14:42
Table of Contents

01. ๐ ๋ฌธ์ ํ์
NxM ํฌ๊ธฐ ๊ณต๊ฐ
์น์ฆ์ 2๋ณ ์ด์์ด ์ธ๋ถ ๊ณต๊ธฐ์ ์ ์ดํ๋ฉด, ๊ทธ ์น์ฆ๋ 1์๊ฐ ํ ๋
น๊ฒ ๋จ
(๋จ, ์น์ฆ ๋ด๋ถ ๊ณต๊ฐ์ ์ ์ดํ์ง ์์ ๊ฒ์ผ๋ก ํ๋จ)
๋งจ ๊ฐ์ฅ์๋ฆฌ์๋ ์น์ฆ๊ฐ ๋์ด์ง ์์
INPUT
N, M ์
๋ ฅ
NxM ํฌ๊ธฐ๋งํผ ์ํ ์
๋ ฅ (0: ๊ณต๊ธฐ, 1: ์น์ฆ)
OUTPUT
์น์ฆ๊ฐ ๋ชจ๋ ๋
น์ ์์ด์ง๊ฒ ๋๋ ์๊ฐ ์ถ๋ ฅ
01-1. ๊ฐ๋ฅํ ์๊ฐ ๋ณต์ก๋
- N, M <= 100
์ธ๋ถ ๊ณต๊ธฐ์ ๋ง๋ฟ์ ๊ณต๊ฐ ํ์ O(NM)
์น์ฆ์ ๋ํด ํ์ O(NM) - ์น์ฆ ํ์ 1์๊ฐ ๋น, ์ธ๋ถ ๊ณต๊ธฐ ํ์ 1ํ ์งํํ๊ฒ ๋๋ฏ๋ก, O(2NM)์ผ๋ก ๋ณผ ์ ์๋ค. 2e4๋ก 1์ด๋ด ์ถฉ๋ถํ ์ํ๊ฐ๋ฅ
01-2. ์๊ณ ๋ฆฌ์ฆ ์ ํ
- BFS๋ฅผ ์ด์ฉํ์ฌ ์ธ๋ถ ๊ณต๊ธฐ์ ์น์ฆ์ ๋ํด ํ์์ ์๊ตฌํ๋ ์๋ฎฌ๋ ์ด์ ๋ฌธ์
02. ๐ ์ฝ๋ ์ค๊ณํ๊ธฐ
- N, M ๊ทธ๋ฆฌ๊ณ NxM ํฌ๊ธฐ ๊ณต๊ฐ์ ๋ํ ์ํ(์น์ฆ์ ๊ณต๊ธฐ) ์ ๋ ฅ
- ์น์ฆ๊ฐ ์ ๋ ฅ๋๋ฉด, ์ ์ฅ๋๋ ์์น (x, y)๋ฅผ queue์ ์ถ๊ฐ
- (0, 0) ์์น๋ถํฐ BFS๋ฅผ ์ํํ์ฌ ๊ณต๊ธฐ์ธ ๋ถ๋ถ์ is_air(vector)์ ์ ์ฅ
(๊ฐ์ฅ์๋ฆฌ์๋ ์น์ฆ๋ฅผ ๋์ ์ ์๊ธฐ ๋๋ฌธ์, ๊ผญ์ง์ ์ธ (0, 0)๋ถํฐ ํ์) - queue์ ์ ์ฅ๋ ์น์ฆ๋ฅผ ์์ฐจ์ ์ผ๋ก ๊บผ๋ด์ด 4๋ฉด์ ํ์
- is_air๋ฅผ ์กฐ์ฌํ์ฌ 2๋ฉด ์ด์์ด ์ฐธ์ผ ๊ฒฝ์ฐ, ์์ ๊ณต๊ฐ vector์ ํ์ฌ ์น์ฆ๊ฐ ๋ น์ ์ํ๋ฅผ ์ ์ฅ
- ์๋ ๊ฒฝ์ฐ, ๋ค์ ์๊ฐ์ ๋ค์ ์กฐ์ฌํ๊ธฐ ์ํ์ฌ ํ์ฌ ์์น(x, y)๋ฅผ ์์ queue์ ์ถ๊ฐ
- queue์ ์์๋ฅผ ๋ชจ๋ ๊บผ๋ด์ด ํ์ํ์ ๊ฒฝ์ฐ, ์๊ฐ์ 1์๊ฐ ์ฆ๊ฐ์ํค๊ณ ์ํ ๋๊ธฐํ
- queue์ ๋ค์์ ์กฐ์ฌํ ์น์ฆ๋ฅผ ๋ด๊ณ ์๋ ์์ queue์ ๋ฐ์ดํฐ๋ฅผ ๋ณต์ฌํ๊ณ , ์์ queue๋ฅผ ์ด๊ธฐํ
- ์์ ๊ณต๊ฐ vector๋ฅผ ํ์ฌ ๊ณต๊ฐ(๊ณต๊ธฐ์ ์น์ฆ๊ฐ ์ ์ฅ๋ 2์ฐจ์ vector)์ ๋ณต์ฌ
- queue์ ๋ ์ด์ ์กฐ์ฌํ ์น์ฆ๊ฐ ๋จ์์์ง ์์ ๊ฒฝ์ฐ ์์๋ ์๊ฐ์ ๋ฐํํ๊ณ , ์๋๋ฉด 3๋ฒ๋ถํฐ ๋ค์ ์ํ
03. ๐ ๏ธ ์๋๋ณ ์์
1ํ์ฐจ
- ๊ฐ ์น์ฆ์ ๋ํด์ ๋ ์๊ฐ์ ์กฐ์ฌ๋๋ค๋ ๊ฒ์ ์ฝ๋์ ๋ฐ์ํ์ง ๋ชป ํ์
- ์์ vector๋ฅผ ํตํด, ์ง๊ธ๊น์ง์ ์น์ฆ ์ํ๋ฅผ ์ ์ฅํ๊ณ , queue์ ๋ด๊ธด ๋ชจ๋ ์น์ฆ๋ฅผ ์กฐ์ฌ๊ฐ ๋๋๊ณ ์์ vector๋ฅผ ์๋ vector์ ๋ฎ์ด์์ฐ๋ ๊ฒ์ผ๋ก ์ํ๋ฅผ ๋๊ธฐํ, (๋ค์์ ์กฐ์ฌํ ์น์ฆ๋ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๋๊ธฐํ)
2ํ์ฐจ
04. ๐๏ธ ์ ๋ต ์ฝ๋
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<int>> board;
vector<vector<bool>> is_air;
queue<pair<int, int>> cheese;
queue<pair<int, int>> next_cheese;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
bool is_valid(int x, int y) {return (x >= 0 && x < N && y >= 0 && y < M);}
void mark_air(){
vector<vector<bool>> visited(N, vector<bool>(M, false));
queue<pair<int, int>> q;
is_air.assign(N, vector<bool>(M, false));
q.emplace(0, 0);
visited[0][0] = true;
while(!q.empty()){
auto& [x, y] = q.front(); q.pop();
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(!is_valid(nx, ny) || board[nx][ny] == 1 || visited[nx][ny]) continue;
visited[nx][ny] = true;
is_air[nx][ny] = true;
q.emplace(nx, ny);
}
}
}
int solution(){
vector<vector<int>> tmp_board(N, vector<int>(M));
tmp_board = board;
int hours = 0;
mark_air();
while(!cheese.empty()){
auto& [x, y] = cheese.front(); cheese.pop();
int cnt_air = 0;
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(!is_valid(nx, ny)) continue;
if(board[nx][ny] == 0 && is_air[nx][ny]) cnt_air++;
}
if(cnt_air >= 2) tmp_board[x][y] = 0;
else next_cheese.emplace(x, y);
if(cheese.empty()){
hours++;
if(!next_cheese.empty()){
cheese = next_cheese;
next_cheese = queue<pair<int, int>>();
}
board = tmp_board;
mark_air();
}
}
return hours;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
board.resize(N, vector<int>(M));
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++){
cin >> board[i][j];
if(board[i][j] == 1) cheese.emplace(i, j);
}
cout << solution() << "\n";
return 0;
}'Problem Solving > Baekjoon Online Judge (BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [C++] BOJ 12850 - ๋ณธ๋ ์ฐ์ฑ 2 (0) | 2025.04.20 |
|---|---|
| [C++] BOJ 11444 - ํผ๋ณด๋์น ์ 6 (0) | 2025.04.20 |
| [C++] BOJ 9019 - DSLR (0) | 2025.04.19 |
| [C++] BOJ 23289 - ์จํ๊ธฐ ์๋ ! (0) | 2025.04.18 |
| [C++] BOJ 17837 - ์๋ก์ด ๊ฒ์ 2 (0) | 2025.04.17 |
@ONE_ :: ์ ํธ์
์๋ชป๋ ์ ๋ณด๊ฐ ์๋ค๋ฉด ๋ง์ํด์ฃผ์ธ์!