2024. 2. 1. 17:54ใCoding Test/BOJ
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
๋ด๋ถ ๊ณต๊ธฐ์ ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ์ด๋ป๊ฒ ๊ตฌ๋ณํ ๊น ๊ณ ๋ฏผํ์ต๋๋ค. ๋ฌธ๋ ๋ ์ค๋ฅธ ์๊ฐ์ ๋ค๊ฐํ ์ธ๋ถ ๋ด๋ถ ํ๋ณ ์๊ณ ๋ฆฌ์ฆ์ด์๋๋ฐ, ์น์ฆ๊ฐ ๋ค๊ฐํ์ด ์๋ ์๋ ์๊ธฐ ๋๋ฌธ์ ์ ์ฉํ ์๊ฐ ์๋๋ผ๊ตฌ์.
๊ทธ๋์ ๋ฌธ์ ์์ ์ฃผ์ด์ง ์ ๋ณด์ธ, "๊ฐ์ฅ์๋ฆฌ์๋ ์น์ฆ๊ฐ ๋์ด์ง ์๋๋ค"๋ผ๋ ๊ฑธ ํ์ฉํด ๊ฐ์ฅ์๋ฆฌ์์๋ถํฐ BFS๋ฅผ ํตํด ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ๋ง๋ค์ด์ฃผ๊ธฐ๋ก ๊ฒฐ์ ํ์์ต๋๋ค. ์ด ๋ก์ง์ ์ํด, ์ ๋ ฅ ์ธํ ์ ํ ๋ ์กฐ๊ธ ๋ค๋ฅด๊ฒ ์ธํ ํด์ฃผ์์ต๋๋ค.
// ์ ์ญ ๋ณ์๋ค
#define EXTERNAL_AIR -1
#define AIR -2
#define REMOVE_TARGET_CHEESE -3
using Point = pair<int, int>;
int MaxRow, MaxCol;
vector<vector<int>> Directions{ {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
void Initialize(vector<vector<int>>& field)
{
for (int row = 0; row < MaxRow; row++)
{
for (int col = 0; col < MaxCol; col++)
{
cin >> field[row][col];
if (field[row][col] == 0) field[row][col] = AIR;
else if (field[row][col] == 1) field[row][col] = 0; // ์น์ฆ๊ฐ ์ธ๋ถ ๊ณต๊ธฐ์ ์ ์ดํ๋ ์นธ ์ (์ฐ์ ์ด๊น๊ฐ์ ๋ชจ๋ 0์ผ๋ก)
}
}
}
๊ธฐ์กด ์ ๋ ฅ์์ ๊ณต๊ธฐ์๋ ์นธ(0)์ AIR(-2)๋ก, ์น์ฆ์๋ ์นธ(1)์ 0์ผ๋ก ์ธํ ํด์ฃผ์์ต๋๋ค. ์ด๋ ์น์ฆ๊ฐ ์๋ ์นธ์ ์ ๋ณด๋ฅผ ์ธ๋ถ ๊ณต๊ธฐ๊ฐ ์ ์ดํ๋ ๋ณ์ ์๋ก ์ค์ ํด์ฃผ๊ธฐ ์ํจ์ ๋๋ค. ๋ด๋ถ ๊น์ํ ์๋ ์น์ฆ์ ๊ฒฝ์ฐ์๋ ์ธ๋ถ ๊ณต๊ธฐ์ ์ ์ดํ์ง ์์ 0์ด๋ ๊ฐ์ ๊ฐ๊ธฐ ๋๋ฌธ์, ๊ณต๊ธฐ์ ์ด๋ฅผ ๊ตฌ๋ณํด์ฃผ๊ธฐ ์ํด ๋ค๋ฅด๊ฒ ์ธํ ํ์์ต๋๋ค.
๊ทธ ํ, ๊ฐ์ฅ์๋ฆฌ์๋ ์น์ฆ๊ฐ ์๊ธฐ ๋๋ฌธ์ (0, 0) ์์น์์๋ถํฐ BFS ๋ฐฉ์์ ํตํด ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ์ธํ ํด์ฃผ์์ต๋๋ค.
๊ทธ ๊ณผ์ ์์ ์น์ฆ๋ฅผ ๋ง๋๊ฒ ๋๋ฉด, ํด๋น ์น์ฆ์ ๊ฐ์ 1 ์ฆ๊ฐ์์ผ์ฃผ์์ต๋๋ค.
๋ง์ฝ ํด๋น ์น์ฆ์ ๊ฐ์ด 2 ์ด์์ด๋ผ๋ฉด, ์ ๊ฑฐํ ๋์์ผ๋ก ๊ฐ์ฃผํ๊ณ ๋ณ๋์ ํ(Queue)์ ๋ด์์ฃผ์์ต๋๋ค.
void FillExternalAirAndFindRemovingCheeses(vector<vector<int>>& field, queue<Point>& removeTargetCheeses, int startRow, int startCol)
{
queue<Point> positions;
field[startRow][startCol] = EXTERNAL_AIR;
positions.emplace(Point(startRow, startCol));
while (!positions.empty())
{
int row = positions.front().first;
int col = positions.front().second;
positions.pop();
for (const vector<int>& direction : Directions)
{
int nextRow = row + direction[0];
int nextCol = col + direction[1];
if (nextRow < 0 or nextRow >= MaxRow or nextCol < 0 or nextCol >= MaxCol)
continue;
if (field[nextRow][nextCol] == EXTERNAL_AIR or field[nextRow][nextCol] == REMOVE_TARGET_CHEESE)
continue;
if (field[nextRow][nextCol] >= 0) // ์น์ฆ์ผ ๊ฒฝ์ฐ, ์ธ๋ถ ์ ์ด ๊ณต๊ธฐ ๊ฐ์๋ฅผ ๋๋ ค์ฃผ๊ธฐ
{
field[nextRow][nextCol]++;
if (field[nextRow][nextCol] >= 2)
{
field[nextRow][nextCol] = REMOVE_TARGET_CHEESE;
removeTargetCheeses.emplace(Point(nextRow, nextCol));
}
continue;
}
field[nextRow][nextCol] = EXTERNAL_AIR;
positions.emplace(Point(nextRow, nextCol));
}
}
}
์ด๋ ๊ฒ ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ์ฑ์ฐ๊ณ ์ ๊ฑฐํ ์น์ฆ๋ค์ ์ฐพ์๋ค๋ฉด, ์น์ฆ๋ฅผ ์ ๊ฑฐํ๊ณ ์ ๊ฑฐํ ์๋ฆฌ์์ ๋ค์ ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ํผํธ๋ฆฌ๊ณ , ๊ทธ ๊ณผ์ ์์ ๋ ์ ๊ฑฐํ ์น์ฆ๋ฅผ ์ฐพ๊ณ ... ์ด ๊ณผ์ ์ ๋ ์ด์ ์ ๊ฑฐํ ์น์ฆ๊ฐ ์์ ๋๊น์ง ๋ฐ๋ณตํด์ค๋๋ค.
void Update(vector<vector<int>>& field, queue<Point>& removeTargetCheeses)
{
int size = removeTargetCheeses.size();
for (int i = 0; i < size; i++)
{
Point cheesePosition = removeTargetCheeses.front();
int row = cheesePosition.first;
int col = cheesePosition.second;
removeTargetCheeses.pop();
FillExternalAirAndFindRemovingCheeses(field, removeTargetCheeses, row, col);
}
}
์์ ํจ์๋ค์ main() ํจ์์์ ์ ๋์ํ๋๋ก ์คํํด์ฃผ๋ฉด ๋์ ๋๋ค.
int main()
{
FAST_IO
cin >> MaxRow >> MaxCol;
// 1. ์
๋ ฅ ๋ฐ๊ณ ์ธํ
vector<vector<int>> field(MaxRow, vector<int>(MaxCol));
Initialize(field);
// 2. ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ์ฑ์ฐ๊ณ , ๊ทธ ๊ณผ์ ์์ ์ ๊ฑฐํ ์น์ฆ๋ค ์ฐพ๊ธฐ
queue<Point> removeTargetCheeses;
FillExternalAirAndFindRemovingCheeses(field, removeTargetCheeses, 0, 0); // ๊ฐ์ฅ์๋ฆฌ์๋ ์น์ฆ๊ฐ ์๋ค๊ณ ํ์ผ๋ฏ๋ก (0, 0)๋ถํฐ ์์
// 3. ๋ ์ด์ ์ ๊ฑฐํ ์น์ฆ๊ฐ ์์ ๋๊น์ง ๋ฐ๋ณต
int time = 0;
while (!removeTargetCheeses.empty())
{
Update(field, removeTargetCheeses);
time++;
}
cout << time;
return 0;
}
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using Point = pair<int, int>;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define EXTERNAL_AIR -1
#define AIR -2
#define REMOVE_TARGET_CHEESE -3
int MaxRow, MaxCol;
vector<vector<int>> Directions{ {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
void FillExternalAirAndFindRemovingCheeses(vector<vector<int>>& field, queue<Point>& removeTargetCheeses, int startRow, int startCol)
{
queue<Point> positions;
field[startRow][startCol] = EXTERNAL_AIR;
positions.emplace(Point(startRow, startCol));
while (!positions.empty())
{
int row = positions.front().first;
int col = positions.front().second;
positions.pop();
for (const vector<int>& direction : Directions)
{
int nextRow = row + direction[0];
int nextCol = col + direction[1];
if (nextRow < 0 or nextRow >= MaxRow or nextCol < 0 or nextCol >= MaxCol)
continue;
if (field[nextRow][nextCol] == EXTERNAL_AIR or field[nextRow][nextCol] == REMOVE_TARGET_CHEESE)
continue;
if (field[nextRow][nextCol] >= 0) // ์น์ฆ์ผ ๊ฒฝ์ฐ, ์ธ๋ถ ์ ์ด ๊ณต๊ธฐ ๊ฐ์๋ฅผ ๋๋ ค์ฃผ๊ธฐ
{
field[nextRow][nextCol]++;
if (field[nextRow][nextCol] >= 2)
{
field[nextRow][nextCol] = REMOVE_TARGET_CHEESE;
removeTargetCheeses.emplace(Point(nextRow, nextCol));
}
continue;
}
field[nextRow][nextCol] = EXTERNAL_AIR;
positions.emplace(Point(nextRow, nextCol));
}
}
}
void Update(vector<vector<int>>& field, queue<Point>& removeTargetCheeses)
{
int size = removeTargetCheeses.size();
for (int i = 0; i < size; i++)
{
Point cheesePosition = removeTargetCheeses.front();
int row = cheesePosition.first;
int col = cheesePosition.second;
removeTargetCheeses.pop();
FillExternalAirAndFindRemovingCheeses(field, removeTargetCheeses, row, col);
}
}
void Initialize(vector<vector<int>>& field)
{
for (int row = 0; row < MaxRow; row++)
{
for (int col = 0; col < MaxCol; col++)
{
cin >> field[row][col];
if (field[row][col] == 0) field[row][col] = AIR;
else if (field[row][col] == 1) field[row][col] = 0; // ์น์ฆ๊ฐ ์ธ๋ถ ๊ณต๊ธฐ์ ์ ์ดํ๋ ์นธ ์ (์ฐ์ ์ด๊น๊ฐ์ ๋ชจ๋ 0์ผ๋ก)
}
}
}
int main()
{
FAST_IO
cin >> MaxRow >> MaxCol;
// 1. ์
๋ ฅ ๋ฐ๊ณ ์ธํ
vector<vector<int>> field(MaxRow, vector<int>(MaxCol));
Initialize(field);
// 2. ์ธ๋ถ ๊ณต๊ธฐ๋ฅผ ์ฑ์ฐ๊ณ , ๊ทธ ๊ณผ์ ์์ ์ ๊ฑฐํ ์น์ฆ๋ค ์ฐพ๊ธฐ
queue<Point> removeTargetCheeses;
FillExternalAirAndFindRemovingCheeses(field, removeTargetCheeses, 0, 0); // ๊ฐ์ฅ์๋ฆฌ์๋ ์น์ฆ๊ฐ ์๋ค๊ณ ํ์ผ๋ฏ๋ก (0, 0)๋ถํฐ ์์
// 3. ๋ ์ด์ ์ ๊ฑฐํ ์น์ฆ๊ฐ ์์ ๋๊น์ง ๋ฐ๋ณต
int time = 0;
while (!removeTargetCheeses.empty())
{
Update(field, removeTargetCheeses);
time++;
}
cout << time;
return 0;
}
'Coding Test > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 1644๋ฒ | ์์์ ์ฐ์ํฉ (C++) (1) | 2024.02.07 |
---|---|
[BOJ] 13904๋ฒ | ๊ณผ์ (C++) (1) | 2024.02.06 |
[BOJ] 3055๋ฒ | ํ์ถ (C++) (1) | 2024.01.31 |
[BOJ] 2357๋ฒ | ์ต์๊ฐ๊ณผ ์ต๋๊ฐ (C++) (1) | 2024.01.24 |
[BOJ] 2637๋ฒ | ์ฅ๋๊ฐ ์กฐ๋ฆฝ (C++) (0) | 2023.12.21 |