
๐ ๋ฌธ์

๐ฑ ์์ด๋์ด
BFS๋ก ๊ฐ ์ฌ์ ๋ฒํธ๋ฅผ ๋ถ์ฌํ๊ณ , ๋ชจ๋ ์ฌ์ ์ฐ๊ฒฐํ๋ ๋ค๋ฆฌ์๊ธฐ(MST)๋ก ๊ตฌํํ์
2์ฐจ์ ํ๋ฉด ์ ์ฌ๊ณผ ๋ฐ๋ค๊ฐ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๊ณ , ๋ค๋ฆฌ๋ฅผ ์ฐ๊ฒฐํด์ ๋ชจ๋ ์ฌ์ ์ฐ๊ฒฐํ๋ ค๊ณ ํ๋ค.
๋ค๋ฆฌ๋ ๊ฐ์ฅ ์งง๊ฒ ์ฐ๊ฒฐํด์ผ ํ๋ค. ์ฆ ๋ชจ๋ ์ฌ์ ์ต์๋น์ฉ์ผ๋ก ๋ชจ๋ ์๋๋ค๋ฉด = ์ต์์คํจ๋์์ ์ ์ถํด์ผ ํ ์ ์๋ค.
์๋์ ๊ฐ์ 3๋จ๊ณ๋ฅผ ๊ฑฐ์ณ์ ๊ตฌํํ๋ค.
1. ๋ชจ๋ ์ฌ์ ํ์ํ๊ณ ๋ฒํธ๋ฅผ ๋ถ์ฌํ๋ค ๊ทธ๋์ผ A -> B๋ฅผ ํน์ ํ ์ ์๋ค.
2. A -> B๊ฐ ์ง์ ์ผ๋ก ์ฅ์ ๋ฌผ ์์ด ๋ค๋ฆฌ๋ฅผ ์ด์ ์ ์๋์ง ํ๋จ ํ ์ฐ์ ์์ํ์ ์์๋ ธ๋, ๋ชฉํ๋ ธ๋, ๋น์ฉ์ ์ ๋ ฅํ๊ธฐ
3. ํฌ๋ฃจ์ค์นผ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ชจ๋ ์ฌ์ ์๊ธฐ
๋งต์ ํฌ๊ธฐ, ์ฌ์ ๊ฐ์๊ฐ ๋งค์ฐ ์๋ค, ์๊ฐ์ด๊ณผ๋ฅผ ๊ฑฑ์ ํ๋ ๊ฒ๋ณด๋ค ์ฐจ๊ทผ์ฐจ๊ทผ ๊ตฌํ์ ์ง์คํ๋ ๊ฒ ์ข์ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ๋ค!
๐ฑ ์ฝ๋ ๋ฐ ํ์ด
| public class Main { | |
| public static int N, M; | |
| public static int[] parent; | |
| public static int[][] arr1; | |
| public static boolean[][] vi; | |
| public static int[] dx = {0, 0, -1, 1}; | |
| public static int[] dy = {-1, 1, 0, 0}; | |
| public static PriorityQueue<mstNode> pq = new PriorityQueue<>(); | |
| public static class node { | |
| int x, y; | |
| public node(int x, int y) { | |
| this.x = x; | |
| this.y = y; | |
| } | |
| } | |
| // MST์ ์ฌ์ฉํ ํด๋์ค | |
| public static class mstNode implements Comparable<mstNode> { | |
| int x, y, w; | |
| public mstNode(int x, int y, int w) { | |
| this.x = x; | |
| this.y = y; | |
| this.w = w; | |
| } | |
| @Override | |
| public int compareTo(mstNode o) { | |
| return w - o.w; | |
| } | |
| } | |
| // ๋ถ๋ชจ ์ฐพ๊ธฐ | |
| public static int find(int x) { | |
| if (parent[x] == x) return x; | |
| return parent[x] = find(parent[x]); | |
| } | |
| // ์งํฉ ํฉ์น๊ธฐ | |
| public static boolean union(int x, int y) { | |
| int from = find(x); | |
| int to = find(y); | |
| if (from != to) { | |
| parent[to] = from; | |
| return false; | |
| } | |
| return true; | |
| } | |
| // ์ฌ๋ค ์ฌ์ด ๋ค๋ฆฌ ์๊ธฐ | |
| public static void makeBridge(int x, int y) { | |
| int nowIsland = arr1[y][x]; | |
| for (int i = 0; i < 4; i++) { | |
| int cnt = 0; | |
| for (int j = 1; j <= 10; j++) { | |
| int nx = x + j * dx[i]; | |
| int ny = y + j * dy[i]; | |
| if (nx < 0 || nx >= M || ny < 0 || ny >= N) break; | |
| int endIsland = arr1[ny][nx]; | |
| if (nowIsland == endIsland) break; // ๊ฐ์ ์ฌ๋ผ๋ฆฌ ์ถฉ๋์ break; | |
| if (arr1[ny][nx] == 0) { // ๋ฐ๋ค๋ผ๋ฉด ์นด์ดํ | |
| cnt++; | |
| } else { // ๋ค๋ฅธ์ฌ ๋์ฐฉ์ | |
| if (cnt >= 2) { // ๋ค๋ฆฌ์ ๊ธธ์ด๊ฐ 2 ์ด์์ผ๋ ์ฐ์ ์์ ํ ์ ๋ ฅ | |
| pq.offer(new mstNode(nowIsland, endIsland, cnt)); | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| // ์ฌ์ ๋ฒํธ ๋ถ์ฌํ๊ธฐ | |
| public static void bfs(int x, int y, int flagN) { | |
| Queue<node> qu = new LinkedList<>(); | |
| qu.offer(new node(x, y)); | |
| vi[y][x] = true; | |
| arr1[y][x] = flagN; | |
| while (!qu.isEmpty()) { | |
| node nd = qu.poll(); | |
| for (int i = 0; i < 4; i++) { | |
| int nx = nd.x + dx[i]; | |
| int ny = nd.y + dy[i]; | |
| if (nx < 0 || nx >= M || ny < 0 || ny >= N) continue; | |
| if (!vi[ny][nx] && arr1[ny][nx] == 1) { | |
| vi[ny][nx] = true; | |
| arr1[ny][nx] = flagN; // ๋ฒํธ ๋ฃ๊ธฐ | |
| qu.offer(new node(nx, ny)); | |
| } | |
| } | |
| } | |
| } | |
| public static void main(String[] args) throws IOException { | |
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
| StringTokenizer st; | |
| st = new StringTokenizer(br.readLine()); | |
| N = Integer.parseInt(st.nextToken()); | |
| M = Integer.parseInt(st.nextToken()); | |
| int cnt = 0; | |
| arr1 = new int[N][M]; | |
| parent = new int[7]; | |
| vi = new boolean[N][M]; | |
| for (int i = 0; i < 7; i++) { | |
| parent[i] = i; | |
| } | |
| for (int i = 0; i < N; i++) { | |
| st = new StringTokenizer(br.readLine()); | |
| for (int j = 0; j < M; j++) { | |
| arr1[i][j] = Integer.parseInt(st.nextToken()); | |
| } | |
| } | |
| // ์ฌ๋ง๋ค ๋ฒํธ ๋ถ์ฌ | |
| for (int i = 0; i < N; i++) { | |
| for (int j = 0; j < M; j++) { | |
| if (!vi[i][j] && arr1[i][j] == 1) { | |
| bfs(j, i, ++cnt); | |
| } | |
| } | |
| } | |
| // ์ฌ๊ณผ ์ฌ ์ฌ์ด ์ผ์ง์ ์ผ๋ก ๋ค๋ฆฌ ์๊ธฐ | |
| for (int i = 0; i < N; i++) { | |
| for (int j = 0; j < M; j++) { | |
| if (arr1[i][j] != 0) { | |
| makeBridge(j, i); | |
| } | |
| } | |
| } | |
| // ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์ฌ ์๊ธฐ | |
| int answer = 0; | |
| int cntIsland = 1; | |
| while (!pq.isEmpty()) { | |
| mstNode now = pq.poll(); | |
| if (!union(now.x, now.y)) { | |
| cntIsland++; | |
| answer += now.w; | |
| } | |
| } | |
| // ๋ชจ๋ ์ฌ์ ์ด์๋ค๋ฉด ์ถ๋ ฅ | |
| System.out.println(cnt == cntIsland ? answer : -1); | |
| } | |
| } |
๐ฑ ๋๋ ์

MST + BFS ๊ตฌํ๋ฌธ์ ๋ผ์ ์ฐจ๊ทผ์ฐจ๊ทผ ํ์๋๋ 1ํธ ๋ง์ ์๋ฐ 8 ๊ธฐ์ค 1๋ฑ ๋จน์๋ค์ ๋ง์์ต๋๋ค ใ ใ
์ฑ๋ฅ๋ ๊ฐ์ด ์๊ฐํ๋ฉด์ ์๊ณ ๋ฆฌ์ฆ ์ฝ๋ฉํ๋ ๊ฒ ์ต๊ด์ด ๋๋ฏํ ๋๋์ด๋ค์^^
'Algorithm > - Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [BOJ] ๋ฐฑ์ค 16985 Maaaaaaaaaze (JAVA) (2) | 2024.08.14 |
|---|---|
| [BOJ] ๋ฐฑ์ค 20157 ํ์ด์ ์์! (JAVA) (0) | 2024.07.07 |
| [BOJ] ๋ฐฑ์ค 11376 ์ดํ๊ฐํธ 2 (JAVA) (0) | 2024.06.28 |
| [BOJ] ๋ฐฑ์ค 23559 ๋ฐฅ (JAVA) (0) | 2024.06.21 |
| [BOJ] ๋ฐฑ์ค 19577 ์ํ์ ์ฌ๋ฐ์ด (JAVA) (0) | 2024.05.26 |
๋๋ง์ ๊ฐ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค!
ํฌ์คํ ์ด ์ข์๋ค๋ฉด "์ข์์โค๏ธ" ๋๋ "๊ตฌ๋ ๐๐ป" ํด์ฃผ์ธ์!