-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2146.java
More file actions
139 lines (127 loc) ยท 4.69 KB
/
Q2146.java
File metadata and controls
139 lines (127 loc) ยท 4.69 KB
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
134
135
136
137
138
139
/**
* Date: 2018. 9. 1.
* Author: inhyuck | https://github.com/inhyuck
* Solution URL: https://github.com/skhucode/skhucode-inhyuck
* Title: ๋ค๋ฆฌ ๋ง๋ค๊ธฐ
* Problem: ์ง๋๊ฐ ์ฃผ์ด์ง ๋, ๊ฐ์ฅ ์งง์ ๋ค๋ฆฌ ํ๋๋ฅผ ๋์ ๋ ๋๋ฅ์ ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์ฐพ์ผ์์ค.
* URL: https://www.acmicpc.net/problem/2146
*/
package io.inhyuck.graph;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Q2146 {
static int[][] list;
static boolean[][] check;
static int n;
static Queue<Island> queue;
static Island[][] islands;
static int minNumber;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
list = new int[n][n];
check = new boolean[n][n];
islands = new Island[n][n]; //์ฌ ํ๋ ฌ์ ํตํด ๋ด๋ถ ์ํ ๊ธฐ๋ก.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
list[i][j] = scanner.nextInt();
}
}
int islandNumber = 1; //์ฌ๋ผ๋ฆฌ ๊ตฌ๋ถ์ง๊ธฐ ์ํ ๊ณ ์ ๋ฒํธ (๊ฐ์ ์ฌ๋ผ๋ฆฌ๋ ๊ฐ์ ๋ฒํธ ๋ถ์ฌ๋จ)
queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (check[i][j] == false && list[i][j] == 1) {
dfs(i, j, islandNumber++);
}
}
}
// for (int i = 0; i < n; i++) {
// System.out.println(Arrays.toString(islands[i]));
// }
int i, j, step;
minNumber = Integer.MAX_VALUE; //์ฌ ์ฌ์ด์ ๊ฑฐ๋ฆฌ ์ต์๊ฐ
while (!queue.isEmpty()) { //BFS
Island island = queue.poll();
i = island.i;
j = island.j;
step = island.step;
islandNumber = island.islandNumber;
if (i > 0 && isOkForBfs(i - 1, j, step, islandNumber)) {
insertQueue(queue, i - 1, j, step + 1, islandNumber);
}
if (i < n - 1 && isOkForBfs(i + 1, j, step, islandNumber)) {
insertQueue(queue, i + 1, j, step + 1, islandNumber);
}
if (j > 0 && isOkForBfs(i, j - 1, step, islandNumber)) {
insertQueue(queue, i, j - 1, step + 1, islandNumber);
}
if (j < n - 1 && isOkForBfs(i, j + 1, step, islandNumber)) {
insertQueue(queue, i, j + 1, step + 1, islandNumber);
}
}
System.out.println(minNumber);
}
private static class Island { //์ฌ ํด๋์ค
int i;
int j;
int step; //์ฌ ๋ด๋ถ๋ก ๋ถํฐ ๊ฑฐ๋ฆฌ
int islandNumber; //์ฌ ๊ณ ์ ๋ฒํธ
public Island(int i, int j, int step, int islandNumber) {
this.i = i;
this.j = j;
this.step = step;
this.islandNumber = islandNumber;
}
@Override
public String toString() {
return "Island{" +
"i=" + i +
", j=" + j +
", step=" + step +
", islandNumber=" + islandNumber +
'}';
}
}
private static void dfs(int i, int j, int islandNumber) {
check[i][j] = true;
queue.add(new Island(i, j, 0, islandNumber));
islands[i][j] = new Island(i, j, 0, islandNumber);
if (i > 0 && isOkForDfs(i-1, j)) {
dfs(i-1, j, islandNumber);
}
if (i < n - 1 && isOkForDfs(i+1, j)) {
dfs(i + 1, j, islandNumber);
}
if (j > 0 && isOkForDfs(i, j-1)) {
dfs(i, j-1, islandNumber);
}
if (j < n - 1 && isOkForDfs(i, j+1)) {
dfs(i, j + 1, islandNumber);
}
}
private static boolean isOkForDfs(int i, int j) {
if (list[i][j] == 1 && check[i][j] == false) {
return true;
}
return false;
}
private static void insertQueue(Queue queue, int i, int j, int step, int islandNumber) {
check[i][j] = true;
islands[i][j] = new Island(i, j, step, islandNumber);
queue.offer(new Island(i, j, step, islandNumber));
}
private static boolean isOkForBfs(int i, int j, int nowStep, int islandNumber) {
//์ด๋ฏธ ํ์ํ์ง๋ง ํด๋น ์ฌ๊ณผ ๊ณ ์ ๋ฒํธ๊ฐ ๋ค๋ฅธ๊ฒฝ์ฐ => ๋ค๋ฅธ์ฌ์์ ๊ทธ ์ ์ ํ์ํ์, ์ฌ ์ฌ์ด๊ฐ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ์ ์๋ค.
if (check[i][j] == true && islandNumber != islands[i][j].islandNumber) {
minNumber = Math.min(minNumber, nowStep + islands[i][j].step);
return false;
}
if (list[i][j] == 0 && check[i][j] == false) {
return true;
}
return false;
}
}