๋ฌธ์ :
programmers.co.kr/learn/courses/30/lessons/62050
๋ถ์ :
๋ฌธ์ ๋ฅผ ๊ตฌํํ๋ฉด ๋๋ ์์ด์๋ค.
ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ, spanning tree ์ฒ์ ๊ณต๋ถํด๋ณด๋๋ฐ ๋ฐฑ์ค์์ ์ ์ ๋ฌธ์ ๋ฅผ ๋ ํ์ด๋ด์ผ๊ฒ ๋ค.
c++ ์ฝ๋ :
//
// [SW] 2019_terrainMove.cpp
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2021/05/02.
//
#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
#include <algorithm>
#include <string.h>
using namespace std;
vector<pair<int, int>> vec[90001];
int visit[301][301];
vector<int> Parent;
int dx[] = { 0, 0, 1, -1 }; //up down left right
int dy[] = { 1, -1, 0, 0 };
vector<pair<int, pair<int, int>>> edge;
int height = 1;
//vector<vector<int>> rands = {{1, 4, 8, 10}, {5, 5, 5, 5}, {10, 10, 10, 10}, {10, 10, 10, 20}};
vector<vector<int>> rands = {{10, 11, 10, 11}, {2, 21, 20, 10}, {1, 20, 21, 11}, {2, 1, 2, 1}};
int k = 1; // ๊ฐ์ ๊ทธ๋ฃน์ธ์ง ํ๋จํ๋ ๊ณ ์ id
//BFS๋ฅผ ์ด์ฉํด ๊ฐ์ ์งํ๋ผ๋ฆฌ ๊ฐ์ id๊ฐ ๊ฐ์ง๋ visit ๋ฐฐ์ด ์ด๊ธฐํ
void tie_same_terrain(int i, int j, int k){
queue<pair<int, int>> q;
q.push({i, j});
visit[i][j] = k; //๊ณ ์ id ๊ฐ์ ๋ฃ์ด์ค๋ค
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= rands.size() || ny >= rands.size()) continue;
if(abs(rands[x][y] - rands[nx][ny]) > height || visit[nx][ny] != 0) continue;
visit[nx][ny] = k;
q.push({nx, ny});
}
}
}
//์ด๋ ๊ฒ !
//1 1 1 1
//1 1 1 1
//2 2 2 2
//2 2 2 3
//for kruskal prepare
int Find_Parent(int A)
{
if (A == Parent[A]) return A;
return Parent[A] = Find_Parent(Parent[A]);
}
void Union(int A, int B)
{
A = Find_Parent(A);
B = Find_Parent(B);
Parent[B] = A;
}
bool Same_Parent(int A, int B)
{
A = Find_Parent(A);
B = Find_Parent(B);
if (A == B) return true;
return false;
}
int main(){
int answer = 0;
memset(visit, 0, sizeof(visit));
for(int i =0; i < rands.size(); i++){
for(int j =0; j < rands[i].size(); j++){
if(visit[i][j] == 0){
tie_same_terrain(i, j, k++);
}
}
}
//visit ๋ฐฐ์ด ํ์ธ์ฉ
// for(int i =0; i < 4; i++){
// for(int j =0; j < 4; j++){
// cout << visit[i][j] << " ";
// }
// cout << '\n';
// }
for(int i =0; i < rands.size(); i++){
for(int j =0; j < rands[i].size(); j++){
for(int s = 0; s < 4; s++){
int nx = i + dx[s];
int ny = j + dy[s];
if(nx >= 0 && ny >= 0 && nx < rands.size() && ny < rands.size()){
if(visit[i][j] != visit[nx][ny]){
//๋น์ฉ, ๋
ธ๋1, ๋
ธ๋2 ์
edge.push_back({abs(rands[nx][ny] - rands[i][j]), {visit[i][j], visit[nx][ny]}});
}
}
}
}
}
//kruskal
Parent.resize(k);
sort(edge.begin(), edge.end()); // ์ต์ ๋น์ฉ ์์ผ๋ก ์ ๋ ฌ
for (int i = 1; i < k; i++) Parent[i] = i; //์๋ก ๋จ์ดํธ๋ฆฌ๊ธฐ ์ํด
for (int i = 0; i < edge.size(); i++)
{
int N1 = edge[i].second.first; // ์ฒซ ๋
ธ๋
int N2 = edge[i].second.second; //๋๋ฒ์งธ ๋
ธ๋
int Cost = edge[i].first; // ์ต์ ๋น์ฉ
if(Same_Parent(N1, N2) == false)
{
Union(N1, N2);
answer = answer + Cost;
}
}
cout << answer <<'\n';
return 0;
}
๊ฒฐ๊ณผ :
* ์ฐธ๊ณ ํ ๋ธ๋ก๊ทธ :
(์ ๋ง ๊ฐ์ฌํฉ๋๋ค ๊พธ๋ฒ )
๋๊ธ