https://www.acmicpc.net/problem/2146
2146번: 다리 만들기
여러 섬으로 이루어진 나라가 있다. 이 나라의 대통령은 섬을 잇는 다리를 만들겠다는 공약으로 인기몰이를 해 당선될 수 있었다. 하지만 막상 대통령에 취임하자, 다리를 놓는다는 것이 아깝다
www.acmicpc.net
문제
풀이
탐색 문제이다. 우선 섬의 갯수를 세고 각 섬마다 다른 숫자로 라벨링을 해준다. (초기1-> 2,3,4, .... )
섬 아무곳에서 출발하여 바다면 움직인거리+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
|
import java.util.*;
import java.io.*;
import java.util.stream.Collectors;
public class Main {
static int N,landNum,minDepth;
static int[][] map;
static int[] dx,dy;
static boolean[][] visit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
init();
for(int i=0;i<N;i++)
map[i] = Arrays.stream(br.readLine().split(" ")).
mapToInt(Integer::parseInt).toArray();
findLand();
// print();
search();
System.out.println(minDepth);
}
static void findLand() {
for(int i=0;i<N;i++)
for(int j=0;j<N;j++) if(map[i][j]==1) makeLandNumber(new Point(j,i));
}
static void makeLandNumber(Point p){
LinkedList<Point> queue = new LinkedList<>();
++landNum;
map[p.y][p.x]=landNum;
queue.add(p);
while (!queue.isEmpty()){
Point cur = queue.poll();
for(int i=0;i<4;i++){
int nextX = dx[i] + cur.x;
int nextY = dy[i] + cur.y;
if(isRange(nextX, nextY) && map[nextY][nextX] == 1) {
queue.add(new Point(nextX,nextY));
map[nextY][nextX]=landNum;
}
}
}
}
static void search() {
for(int i=0;i<N;i++)
for(int j=0;j<N;j++) if(map[i][j]>1) makeBridge(new Point(j,i,0));
}
static void makeBridge(Point p) {
visit = new boolean[N][N];
LinkedList<Point> queue = new LinkedList<>();
queue.add(p);
visit[p.y][p.x] = true;
while (!queue.isEmpty()){
Point cur = queue.poll();
for(int i=0;i<4;i++){
int nextX = dx[i] + cur.x;
int nextY = dy[i] + cur.y;
if(isRange(nextX, nextY) && !visit[nextY][nextX] && map[nextY][nextX] != map[p.y][p.x]) {
visit[nextY][nextX] = true;
if(map[nextY][nextX]==0) queue.add(new Point(nextX,nextY,cur.depth+1)); //바다일때
else minDepth = Math.min(minDepth,cur.depth); //다른 섬에 도착 했을 때
}
}
}
}
static void print(){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++) System.out.print(map[i][j]+" ");
System.out.println();
}
}
static boolean isRange(int x,int y){
return x>=0&&y>=0&&x<N&&y<N;
}
static void init(){
map = new int[N][N];
visit = new boolean[N][N];
dx = new int[] {0,0,1,-1};
dy = new int[] {1,-1,0,0};
landNum=1;
minDepth=Integer.MAX_VALUE;
}
static class Point{
int x,y;
int depth;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point(int x, int y, int depth) {
this.x = x;
this.y = y;
this.depth = depth;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
", depth=" + depth +
'}';
}
}
}
|
cs |
'알고리즘,PS > 백준' 카테고리의 다른 글
[BOJ] 백준 [16236] 아기 상어 JAVA (0) | 2021.10.22 |
---|---|
[BOJ] 백준 [10971] 외판원 순회2 JAVA (0) | 2021.10.21 |
[BOJ] 백준 [9465] 스티커 JAVA (0) | 2021.10.13 |
[BOJ] 백준 [11054] 가장 긴 바이토닉 부분 수열 JAVA (0) | 2021.10.13 |
[BOJ] 백준 [2150] 강한 결합 요소 JAVA (0) | 2021.10.08 |