Skip to content

Commit 8e69090

Browse files
authored
[20250829] BOJ / G1 / 수영장 만들기 / 이종환
1 parent 3f327ba commit 8e69090

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
```java
2+
import java.awt.Point;
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
9+
public class Main {
10+
11+
static boolean[][] visited;
12+
static int[][] arr;
13+
static int[] dy = {-1,0,1,0};
14+
static int[] dx = {0,1,0,-1};
15+
16+
static Queue<Point> q = new LinkedList<>();
17+
static Queue<Point> temp = new LinkedList<>();
18+
19+
static int height,width,ans;
20+
static boolean isAvailable;
21+
22+
23+
24+
public static void main(String[] args) throws IOException {
25+
26+
init();
27+
process();
28+
print();
29+
30+
}
31+
32+
public static void init() throws IOException {
33+
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
34+
StringTokenizer st = new StringTokenizer(br.readLine());
35+
height = Integer.parseInt(st.nextToken());
36+
width = Integer.parseInt(st.nextToken());
37+
ans = 0;
38+
39+
arr = new int[height][width];
40+
visited = new boolean[height][width];
41+
42+
for (int i = 0 ; i < height; i++) {
43+
String[] input = br.readLine().split("");
44+
for (int j = 0; j < width; j++) {
45+
arr[i][j] = Integer.parseInt(input[j]);
46+
System.out.print(arr[i][j]+ " ");
47+
}
48+
System.out.println();
49+
}
50+
51+
}
52+
53+
public static void process() {
54+
for (int water = 2; water <= 9; water++) {
55+
56+
visited = new boolean[height][width];
57+
58+
59+
for (int i = 0; i < height; i++) {
60+
for (int j = 0; j < width; j++) {
61+
if (visited[i][j]) continue;
62+
if (arr[i][j] >= water) continue;
63+
64+
isAvailable = true;
65+
q.clear();
66+
temp.clear();
67+
q.add(new Point(j,i));
68+
temp.add(new Point(j,i));
69+
bfs(water);
70+
71+
if (isAvailable) {
72+
while(!temp.isEmpty()) {
73+
Point p = temp.poll();
74+
75+
ans += water - arr[p.y][p.x];
76+
arr[p.y][p.x] = water;
77+
}
78+
}
79+
80+
}
81+
}
82+
}
83+
84+
}
85+
86+
public static void bfs(int water) {
87+
while(!q.isEmpty()) {
88+
Point p = q.poll();
89+
90+
for (int i = 0; i < 4; i++) {
91+
int ny = p.y + dy[i];
92+
int nx = p.x + dx[i];
93+
94+
95+
if (ny < 0 || nx < 0 || ny >= height || nx >= width) {
96+
isAvailable = false;
97+
continue;
98+
}
99+
100+
if (visited[ny][nx] || arr[ny][nx] >= water) continue;
101+
102+
visited[ny][nx] = true;
103+
q.add(new Point(nx,ny));
104+
temp.add(new Point(nx,ny));
105+
}
106+
}
107+
}
108+
109+
110+
111+
112+
public static void print() {
113+
System.out.println(ans);
114+
}
115+
}
116+
```

0 commit comments

Comments
 (0)