[BOJ] 17472번 다리 만들기 2 - C++

"최소 신장 트리"

Posted by Yongmin on August 26, 2021

문제

다리 만들기 2

풀이

DFS와 MST를 이용한 문제이다. DFS를 이용하여 인접한 땅들을 하나로 묶는 작업을 하고 이 때 섬의 갯수를 counting한다. 그 이후, 상하좌우 한 방향으로만 계속 가게 하여 섬 번호가 다른것이 있으면 distance를 push하고 break를 한다. 그 이후 distance들을 오름차순으로 정렬하여 크루스칼 알고리즘을 사용한다. 이 때 merge한 횟수와 섬의 갯수가 일치하면 모든 섬이 이어졌다는 소리이므로 distance들의 합을 출력하고, 그렇지 않다면 -1을 출력한다.

소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N, M;
int map[11][11];
bool visited[11][11];

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
//상하좌우

int dp[11][11];
int parent[7];

vector<tuple<int, int, int>> result;

int find(int x){
    if(parent[x] < 0) return x;
    return parent[x] = find(parent[x]);
}

bool merge(int x, int y){
    x = find(x);
    y = find(y);
    
    if(x == y) return false;
    
    parent[x] = y;
    
    return true;
}

void find_island(int y, int x, int cnt){
    map[y][x] = cnt;
    visited[y][x] = true;
    
    for(int i = 0; i<4; i++){
        int delta_y = y + dy[i];
        int delta_x = x + dx[i];
        if(delta_y >= 0 && delta_y<N && delta_x>=0 && delta_x<M){
            if(visited[delta_y][delta_x] == false && map[delta_y][delta_x] == 1){
                find_island(delta_y, delta_x, cnt);
            }
        }
    }
}

void find_dist(int island, int y, int x){
    for(int i = 0; i<4; i++){
        int now_y = y;
        int now_x = x;
        int dist = 0;
        while(true){
            now_y += dy[i];
            now_x += dx[i];
            
            if(now_y < 0 || now_y >= N || now_x < 0 || now_x >= M || map[now_y][now_x] == island) break;
            
            if(map[now_y][now_x] != 0){
                result.push_back({dist, island, map[now_y][now_x]});
                break;
            }
            
            dist++;
            
        }
    }
}

        
int main(){
    scanf("%d %d", &N, &M);
    
    for(int i = 0; i<N; i++){
        for(int j = 0; j<M; j++){
            scanf("%d", &map[i][j]);
        }
    }
    
    int cnt = 0;
    for(int i = 0; i<N; i++){
        for(int j = 0; j<M; j++){
            if(visited[i][j] == false && map[i][j] == 1){
                cnt++;
                find_island(i, j, cnt);
            }
        }
    }
    
    for(int i = 0; i<=cnt; i++){
        parent[i] = -1;
    }
    
    for(int i = 0; i<N; i++){
        for(int j = 0; j<M; j++){
            if(map[i][j] != 0){
                find_dist(map[i][j], i, j);
            }
        }
    }
    
    sort(result.begin(), result.end());
    
    
    
    int final_result = 0;
    int merge_cnt = 1;
    for(int i = 0; i<result.size(); i++){
        if(get<0>(result[i]) < 2) continue;
        if(merge(get<1>(result[i]), get<2>(result[i]))){
            final_result += get<0>(result[i]);
            merge_cnt++;
        }
    }
    
    
    if(merge_cnt != cnt){
        printf("-1\n");
    }
    else printf("%d\n", final_result);
    
    
}


# # #