Algorithm/BOJ
[BOJ] 다리 만들기
goakgoak
2020. 9. 7. 18:33
[2146] 다리 만들기
https://www.acmicpc.net/problem/2146
Solution
- 먼저 각 섬을 구분하기 위한 Numbering 작업을 하고
- map[][]의 모든 cell에 대해 바다가 아니면 BFS 탐색을 한다.
- BFS 탐색을 통해 섬의 끝부분에서 다른 섬까지의 최소값을 answer에 저장한다.
소스코드
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
127
128
129
130
131
132
133
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int N, answer;
static int[][] map, visited;
static int[] dx = { 1, -1, 0, 0 };
static int[] dy = { 0, 0, 1, -1 };
static class Dot {
private int x;
private int y;
public Dot(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
N = stoi(in.readLine());
map = new int[N][N];
visited = new int[N][N];
StringTokenizer st;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(in.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = stoi(st.nextToken());
}
}
int num = 2;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (visited[i][j] == 0 && map[i][j] == 1) {
numbering(new Dot(i, j), num++);
}
}
}
answer = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (map[i][j] != 0) {
init();
bfs(new Dot(i, j), map[i][j]);
}
}
}
System.out.println(answer - 2);
}
static void bfs(Dot dot, int num) {
Queue<Dot> q = new LinkedList<>();
q.offer(dot);
visited[dot.x][dot.y] = 1;
while (!q.isEmpty()) {
Dot cur = q.poll();
int nx, ny;
if (map[cur.x][cur.y] != num && map[cur.x][cur.y] != 0) {
answer = Math.min(answer, visited[cur.x][cur.y]);
if (num == 2)
break;
}
for (int i = 0; i < 4; i++) {
nx = cur.x + dx[i];
ny = cur.y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
continue;
if (map[nx][ny] == num || visited[nx][ny] > 0)
continue;
q.offer(new Dot(nx, ny));
visited[nx][ny] = visited[cur.x][cur.y] + 1;
}
}
}
static void numbering(Dot dot, int num) {
Queue<Dot> q = new LinkedList<>();
q.offer(dot);
visited[dot.x][dot.y] = 1;
while (!q.isEmpty()) {
Dot cur = q.poll();
map[cur.x][cur.y] = num;
int nx, ny;
for (int i = 0; i < 4; i++) {
nx = cur.x + dx[i];
ny = cur.y + dy[i];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
continue;
if (map[nx][ny] == 1 && visited[nx][ny] == 0) {
q.offer(new Dot(nx, ny));
visited[nx][ny] = 1;
}
}
}
}
static void init() {
for (int i = 0; i < N; i++) {
Arrays.fill(visited[i], 0);
}
}
static void print(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("-------");
}
static int stoi(String s) {
return Integer.parseInt(s);
}
}
|
cs |