๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Summer/Winter Coding ์ง€ํ˜• ์ด๋™ level 4 MST

by Jouureee 2021. 5. 2.

๋ฌธ์ œ :

programmers.co.kr/learn/courses/30/lessons/62050

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ง€ํ˜• ์ด๋™

[[1, 4, 8, 10], [5, 5, 5, 5], [10, 10, 10, 10], [10, 10, 10, 20]] 3 15 [[10, 11, 10, 11], [2, 21, 20, 10], [1, 20, 21, 11], [2, 1, 2, 1]] 1 18

programmers.co.kr

 

๋ถ„์„ :

๋ฌธ์ œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ์‹์ด์—ˆ๋‹ค.

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜, 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;
}

 

๊ฒฐ๊ณผ :  

 

 

* ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ : 

yabmoons.tistory.com/470

(์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๊พธ๋ฒ…)

๋Œ“๊ธ€