Skip to content

Commit c74248c

Browse files
authored
[20251026] BOJ / G1 / 다리 만들기 2 / 이준희
1 parent 96b3a7f commit c74248c

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int n, m;
7+
static int[][] map;
8+
static boolean[][] visited;
9+
static int[] dx = {-1, 1, 0, 0};
10+
static int[] dy = {0, 0, -1, 1};
11+
static int islandnum = 0;
12+
static List<Edge> edges = new ArrayList<>();
13+
14+
static class Edge implements Comparable<Edge> {
15+
int from, to, dist;
16+
Edge(int f, int t, int d) {
17+
from = f; to = t; dist = d;
18+
}
19+
public int compareTo(Edge o) {
20+
return this.dist - o.dist;
21+
}
22+
}
23+
24+
static int[] parent;
25+
26+
public static void main(String[] args) throws IOException {
27+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
n = Integer.parseInt(st.nextToken());
30+
m = Integer.parseInt(st.nextToken());
31+
32+
map = new int[n][m];
33+
visited = new boolean[n][m];
34+
35+
for (int i = 0; i < n; i++) {
36+
st = new StringTokenizer(br.readLine());
37+
for (int j = 0; j < m; j++) {
38+
map[i][j] = Integer.parseInt(st.nextToken());
39+
}
40+
}
41+
42+
int id = 2;
43+
for (int i = 0; i < n; i++) {
44+
for (int j = 0; j < m; j++) {
45+
if (map[i][j] == 1 && !visited[i][j]) {
46+
findisland(i, j, id);
47+
id++;
48+
}
49+
}
50+
}
51+
islandnum = id - 2;
52+
53+
bridge();
54+
55+
parent = new int[islandnum + 2];
56+
for (int i = 0; i <= islandnum + 1; i++) parent[i] = i;
57+
Collections.sort(edges);
58+
59+
int used = 0;
60+
int total = 0;
61+
62+
for (Edge e : edges) {
63+
if (union(e.from, e.to)) {
64+
total += e.dist;
65+
used++;
66+
if (used == islandnum - 1) break;
67+
}
68+
}
69+
70+
if (used == islandnum - 1)
71+
System.out.println(total);
72+
else
73+
System.out.println(-1);
74+
}
75+
76+
static void findisland(int x, int y, int id) {
77+
Queue<int[]> q = new LinkedList<>();
78+
q.add(new int[]{x, y});
79+
visited[x][y] = true;
80+
map[x][y] = id;
81+
82+
while (!q.isEmpty()) {
83+
int[] now = q.poll();
84+
for (int d = 0; d < 4; d++) {
85+
int nx = now[0] + dx[d];
86+
int ny = now[1] + dy[d];
87+
if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
88+
if (map[nx][ny] == 1 && !visited[nx][ny]) {
89+
visited[nx][ny] = true;
90+
map[nx][ny] = id;
91+
q.add(new int[]{nx, ny});
92+
}
93+
}
94+
}
95+
}
96+
97+
static void bridge() {
98+
for (int i = 0; i < n; i++) {
99+
for (int j = 0; j < m; j++) {
100+
if (map[i][j] >= 2) {
101+
int from = map[i][j];
102+
for (int d = 0; d < 4; d++) {
103+
int nx = i;
104+
int ny = j;
105+
int dist = 0;
106+
107+
while (true) {
108+
nx += dx[d];
109+
ny += dy[d];
110+
if (nx < 0 || ny < 0 || nx >= n || ny >= m) break;
111+
if (map[nx][ny] == from) break;
112+
if (map[nx][ny] == 0) {
113+
dist++;
114+
continue;
115+
}
116+
if (map[nx][ny] >= 2) {
117+
if (dist >= 2) {
118+
edges.add(new Edge(from, map[nx][ny], dist));
119+
}
120+
break;
121+
}
122+
}
123+
}
124+
}
125+
}
126+
}
127+
}
128+
129+
static int find(int x) {
130+
if (x == parent[x]) return x;
131+
return parent[x] = find(parent[x]);
132+
}
133+
134+
static boolean union(int a, int b) {
135+
a = find(a);
136+
b = find(b);
137+
if (a == b) return false;
138+
parent[b] = a;
139+
return true;
140+
}
141+
}
142+
```

0 commit comments

Comments
 (0)