반응형

https://www.acmicpc.net/problem/2146

 

2146번: 다리 만들기

여러 섬으로 이루어진 나라가 있다. 이 나라의 대통령은 섬을 잇는 다리를 만들겠다는 공약으로 인기몰이를 해 당선될 수 있었다. 하지만 막상 대통령에 취임하자, 다리를 놓는다는 것이 아깝다

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 지도를 입력받고, 아직 방문하지 않은 모든 좌표를 돌면서 bfs를 진행해줍니다.

 

2. bfs를 진행하면서 이어져 있는 땅들을 cnt로 바꿔주고, 바다와 만나는 점들을 queue에 넣어줍니다.

 

3. 한번더 bfs를 진행하면서 다른 육지와 이어졌을 때의 d 중 최솟값을 ans에 저장합니다.

 

4. ans를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<vector>
#include<queue>
 
using namespace std;
 
struct node {
    int y, x, d;
};
 
int N;
int arr[100][100];
int visited[100][100];
int dy[] = { -1,1,0,0 };
int dx[] = { 0,0,-1,1 };
int cnt;
int ans = 987654321;
 
void bfs(int y, int x)
{
    int v[100][100= { 0 };
    queue<pair<intint>> q;
    queue<node> qq;
 
    q.push({ y,x });
 
    while (!q.empty()) {
        const int y = q.front().first;
        const int x = q.front().second;
        q.pop();
 
        arr[y][x] = cnt;
 
        for (int i = 0; i < 4; i++) {
            const int yy = y + dy[i];
            const int xx = x + dx[i];
 
            if (yy >= 0 && yy < N && xx >= 0 && xx < N) {
                if (arr[yy][xx] == 1 && visited[yy][xx] != 1) {
                    visited[yy][xx] = 1;
                    q.push({ yy,xx });
                }
                else if (arr[yy][xx] == 0 && v[y][x] != 1) {
                    v[y][x] = 1;
                    qq.push({ y,x,0 });
                }
            }
        }
    }
 
    while (!qq.empty()) {
        const int y = qq.front().y;
        const int x = qq.front().x;
        const int d = qq.front().d;
        qq.pop();
 
        if (d >= ans) return;
 
        for (int i = 0; i < 4; i++) {
            const int yy = y + dy[i];
            const int xx = x + dx[i];
 
            if (yy >= 0 && yy < N && xx >= 0 && xx < N) {
                if (arr[yy][xx] == 0 && v[yy][xx] == 0) {
                    v[yy][xx] = 1;
                    qq.push({ yy,xx,d + 1 });
                }
                else if (arr[yy][xx] != 0 && arr[yy][xx] != cnt) {
                    ans = min(ans, d);
                    return;
                }
            }
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> arr[i][j];
        }
    }
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (arr[i][j] == 1 && visited[i][j] == 0) {
                visited[i][j] = 1;
                cnt--;
                bfs(i, j);
            }
        }
    }
 
    cout << ans;
}
cs
반응형

'백준' 카테고리의 다른 글

[ 백준 ] 3184번 - 양 (C++)  (0) 2023.09.06
[ 백준 ] 3967번 - 매직 스타 (C++)  (0) 2023.09.05
[ 백준 ] 1486번 - 등산 (C++)  (0) 2023.09.03
[ 백준 ] 7299번 - Food Cubes (C++)  (0) 2023.09.02
[ 백준 ] 1339번 - 단어 수학 (C++)  (0) 2023.09.01

+ Recent posts