[BOJ] 17472๋ฒ | ๋ค๋ฆฌ ๋ง๋ค๊ธฐ 2 (C++)
2023. 12. 12. 19:45ใCoding Test/BOJ
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
BFS + DFS + ์ต์ ์ ์ฅ ํธ๋ฆฌ(MST) ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํด์ผ ํ ์ ์๋, ๋๋จํ ์ฒด๋ ฅ์ ์ผ๋ก ํ๋ ๋ฌธ์ ์์ต๋๋ค. ๊ทธ๋๋ ๋ฌธ์ ์์ ์ฃผ์ด์ง ์กฐ๊ฑด๋ค์ด ๊น๋ค๋กญ์ง ์์, ๊ตฌํ๋ง ์ ๋๋ก ํ๋ค๋ฉด ํต๊ณผ๋ ์ฝ๊ฒ ๋๋ค์. ์ ๊ฐ ํผ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ์ฃผ์ด์ง ์ ๋ ฅ์ 2์ฐจ์ ๋ฐฐ์ด์ ์ ์ฅํ๋, ๋ (1)์ 1์ด ์๋๋ผ ์์ฒญ ํฐ ๋ค๋ฅธ ์ซ์(1์ต)๋ก ๋์ ์ฑ์์ค๋๋ค.
- BFS ์๊ณ ๋ฆฌ์ฆ์ ํตํด, ๊ฐ ์ฌ์ ๋ฒํธ๋ฅผ ํด๋น ์ฌ์ ์์ญ์ ์ฑ์์ค๋๋ค.
- DFS ์๊ณ ๋ฆฌ์ฆ์ ํตํด, ์๋ก ๋ค๋ฅธ ๋ฒํธ์ ์ฌ ๋ผ๋ฆฌ์ ๊ฑฐ๋ฆฌ๋ฅผ ๋ณ๋์ ๋ฐฐ์ด(distances)์ ์ ์ฅํด์ค๋๋ค.
- distances[i][j] ๋ i๋ฒ ์ฌ์์ j๋ฒ ์ฌ์ผ๋ก ๊ฐ๋ ๊ฑฐ๋ฆฌ์ด๋ฉฐ, ์ด๊ธฐ๊ฐ์ ๋งค์ฐ ํฐ ์ซ์(1์ต)์ผ๋ก ์ ์ฅ
- ๊ฑฐ๋ฆฌ๊ฐ 1 ์ดํ์ด๊ฑฐ๋, ๋ค๋ฆฌ๋ฅผ ๊บพ์ด์ ์ง์ด์ผ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์๋ ๊ฑด๋๋ฐ๊ธฐ
- ์ด๋ฏธ ๊ฐ์ด ์ ์ฅ๋์ด ์๋ ๊ฒฝ์ฐ์๋ ์ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ๋ ์์ ๊ฐ์ผ๋ก ๊ฐฑ์
- ์ต์ ์ ์ฅ ํธ๋ฆฌ ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํด, ๊ฐ ์ฌ์ ์๋ ์ต์๊ฐ์ ๊ตฌํด์ ์ถ๋ ฅํฉ๋๋ค.
- ์ ๋ ํ๋ฆผ์ ์๊ณ ๋ฆฌ์ฆ(Prim's Algorithm)์ ์ฌ์ฉํ์ต๋๋ค.
์์ฒ๋ผ ๊ณผ์ ์ ํฌ๊ฒ 4๊ฐ์ง์ธ๋ฐ, ๊ฐ ๊ณผ์ ์ ํจ์๋ก ๊ตฌํํ๊ณ ์ค๊ฐ ํ ์คํธ๋ฅผ ์งํํด ๋ฌธ์ ๊ฐ ์๋์ง ์ ๊ฒํ์์ต๋๋ค. ๊ตฌํ ๋ฌธ์ ๋ ์ด๋ ๊ฒ ์งํํด์ผ, ์๊ฐ์ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ผ๊ตฌ์.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
using Position = pair<int, int>; // <ํ, ์ด>
using Data = pair<int, int>; // <๊ฑฐ๋ฆฌ, ์ฌ ๋ฒํธ>
vector<vector<int>> Directions { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ์ํ์ข์ฐ
const int SEA = 0;
const int MAX = 100000000;
int N, M;
void Input(vector<vector<int>>& field);
void SetIsland(vector<vector<int>>& field, int& islandNumber);
void BFS(vector<vector<int>>& field, const int islandNumber, int row, int col);
void GetIslandDistances(const vector<vector<int>>& field, vector<vector<int>>& distances);
void DFS(const vector<vector<int>>& field, vector<vector<int>>& distances, const vector<int>& direction, int islandNumber, int row, int col, int distance);
int Prim(const vector<vector<int>>& distances, int start);
int main() {
FAST_IO
cin >> N >> M;
int islandNumber = 1;
vector<vector<int>> field(N, vector<int>(M));
Input(field);
SetIsland(field, islandNumber);
vector<vector<int>> distances(islandNumber, vector<int>(islandNumber, MAX));
GetIslandDistances(field, distances);
int answer = Prim(distances, 1);
cout << answer;
return 0;
}
int Prim(const vector<vector<int>>& distances, int start) {
vector<bool> MST(distances.size(), false);
MST[start] = true;
priority_queue<Data, vector<Data>, greater<Data>> edges;
for (int i = 1; i < distances[start].size(); i++) {
if (i == start or distances[start][i] == MAX)
continue;
edges.emplace(distances[start][i], i);
}
int answer = 0;
while (!edges.empty()) {
int distance = edges.top().first;
int islandNumber = edges.top().second;
edges.pop();
if (MST[islandNumber])
continue;
MST[islandNumber] = true;
answer += distance;
for (int i = 1; i < distances[islandNumber].size(); i++) {
if (i == islandNumber or MST[i] or distances[islandNumber][i] == MAX)
continue;
edges.emplace(distances[islandNumber][i], i);
}
}
for (int i = 1; i < MST.size(); i++)
if (!MST[i])
return -1;
return answer;
}
void DFS(const vector<vector<int>>& field, vector<vector<int>>& distances, const vector<int>& direction, int islandNumber, int row, int col, int distance) {
int nextRow = row + direction[0];
int nextCol = col + direction[1];
if (nextRow < 0 or nextRow >= N or nextCol < 0 or nextCol >= M)
return;
if (field[nextRow][nextCol] == islandNumber)
return;
if (field[nextRow][nextCol] == SEA) {
DFS(field, distances, direction, islandNumber, nextRow, nextCol, distance + 1);
return;
}
if (distance <= 1)
return;
int otherIslandNumber = field[nextRow][nextCol];
int newDistance = min(distance, distances[islandNumber][otherIslandNumber]);
distances[islandNumber][otherIslandNumber] = newDistance;
distances[otherIslandNumber][islandNumber] = newDistance;
}
void GetIslandDistances(const vector<vector<int>>& field, vector<vector<int>>& distances) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < M; col++) {
if (field[row][col] == SEA)
continue;
for (const auto& direction : Directions)
DFS(field, distances, direction, field[row][col], row, col, 0);
}
}
}
void Input(vector<vector<int>>& field) {
for (int row = 0; row < N; row++)
for (int col = 0; col < M; col++) {
int value;
cin >> value;
value = (value == 1) ? MAX : value;
field[row][col] = value;
}
}
void SetIsland(vector<vector<int>>& field, int& islandNumber) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < M; col++) {
if (field[row][col] == SEA or field[row][col] < islandNumber)
continue;
BFS(field, islandNumber++, row, col);
}
}
}
void BFS(vector<vector<int>>& field, const int islandNumber, int row, int col) {
queue<Position> positions;
positions.emplace(row, col);
field[row][col] = islandNumber;
while (!positions.empty()) {
int currentRow = positions.front().first;
int currentCol = positions.front().second;
positions.pop();
for (const auto& direction : Directions) {
int nextRow = currentRow + direction[0];
int nextCol = currentCol + direction[1];
if (nextRow < 0 or nextRow >= N or nextCol < 0 or nextCol >= M)
continue;
if (field[nextRow][nextCol] == SEA or field[nextRow][nextCol] == islandNumber)
continue;
field[nextRow][nextCol] = islandNumber;
positions.emplace(nextRow, nextCol);
}
}
}
728x90
๋ฐ์ํ
'Coding Test > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 1987๋ฒ | ์ํ๋ฒณ (C++) (0) | 2023.12.13 |
---|---|
[BOJ] 14502๋ฒ | ์ฐ๊ตฌ์ (C++) (0) | 2023.12.13 |
[BOJ] 13549๋ฒ | ์จ๋ฐ๊ผญ์ง 3 (C++) (1) | 2023.12.06 |
[BOJ] 16928๋ฒ | ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์ (C++) (0) | 2023.12.01 |
[BOJ] 9935๋ฒ | ๋ฌธ์์ด ํญ๋ฐ (C++) (1) | 2023.10.08 |