Skip to content

Commit 754cc21

Browse files
authored
[20251015] BOJ / G3 / 치즈 / 이종환
1 parent e15a6d9 commit 754cc21

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

0224LJH/202510/15 BOJ 치즈.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
import java.awt.Point;
7+
8+
public class Main {
9+
10+
static int height, width, round,ans;
11+
static int[][] arr;
12+
static Queue<Point> cheeses = new LinkedList<>();
13+
14+
static int[] dy = { -1,0,1,0};
15+
static int[] dx = { 0,1,0,-1};
16+
17+
static final int OUTSIDE = 0;
18+
static final int CHEESE = 1;
19+
static final int INSIDE = 2;
20+
21+
22+
public static void main(String[] args) throws NumberFormatException, IOException {
23+
init();
24+
process();
25+
print();
26+
}
27+
28+
29+
30+
public static void init() throws NumberFormatException, IOException {
31+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
32+
StringTokenizer st = new StringTokenizer(br.readLine());
33+
height = Integer.parseInt(st.nextToken());
34+
width = Integer.parseInt(st.nextToken());
35+
arr = new int[height][width];
36+
37+
for (int i = 0; i < height; i++) {
38+
st = new StringTokenizer(br.readLine());
39+
for (int j = 0; j < width; j++) {
40+
arr[i][j] = Integer.parseInt(st.nextToken());
41+
}
42+
}
43+
44+
}
45+
46+
public static void process() throws IOException {
47+
48+
findInside();
49+
int second = 0;
50+
while (!cheeses.isEmpty()) {
51+
melt();
52+
second++;
53+
}
54+
55+
56+
ans = second;
57+
}
58+
59+
private static void melt() {
60+
Queue<Point> cheeseLeft = new LinkedList<>();
61+
Queue<Point> deleteQ = new LinkedList<>();
62+
63+
while(!cheeses.isEmpty()) {
64+
Point p = cheeses.poll();
65+
66+
if(isNotMelting(p.y , p.x)) {
67+
cheeseLeft.add(p);
68+
continue;
69+
}
70+
deleteQ.add(p);
71+
}
72+
73+
while(!deleteQ.isEmpty()) {
74+
Point p = deleteQ.poll();
75+
76+
arr[p.y][p.x] = OUTSIDE;
77+
78+
for (int i = 0; i <4 ; i++) {
79+
int ny = p.y + dy[i];
80+
int nx = p.x + dx[i];
81+
if ( ny < 0 || nx < 0 || ny >= height || nx >= width
82+
|| arr[ny][nx] == CHEESE || arr[ny][nx] == OUTSIDE) continue;
83+
84+
arr[ny][nx] = OUTSIDE;
85+
Point nPoint = new Point(nx,ny);
86+
87+
Queue<Point> insideCheese = new LinkedList<>();
88+
89+
insideCheese.add(nPoint);
90+
while(!insideCheese.isEmpty()) {
91+
Point iP =insideCheese.poll();
92+
for (int j = 0; j < 4; j++) {
93+
int nIy = iP.y + dy[j];
94+
int nIx = iP.x + dx[j];
95+
if ( nIy < 0 || nIx < 0 || nIy >= height || nIx >= width
96+
|| arr[nIy][nIx] == CHEESE || arr[nIy][nIx] == OUTSIDE) continue;
97+
98+
arr[nIy][nIx] = OUTSIDE;
99+
insideCheese.add(new Point(nIx, nIy));
100+
}
101+
}
102+
}
103+
}
104+
105+
cheeses = cheeseLeft;
106+
107+
108+
}
109+
private static boolean isNotMelting(int y,int x) {
110+
111+
int outSideCnt = 0;
112+
113+
for (int i = 0 ; i < 4; i++) {
114+
int ny = y + dy[i];
115+
int nx = x + dx[i];
116+
if ( ny < 0 || nx < 0 || ny >= height || nx >= width || arr[ny][nx] == OUTSIDE) outSideCnt++;
117+
}
118+
// System.out.println(y + " " + x + " -> "+ outSideCnt);
119+
120+
return (outSideCnt < 2) ;
121+
}
122+
123+
private static void findInside() {
124+
boolean[][] visited = new boolean[height][width];
125+
126+
for (int i = 0; i < height; i++) {
127+
for (int j = 0; j < width; j++) {
128+
if (visited[i][j] ) continue;
129+
if (arr[i][j] == CHEESE) {
130+
cheeses.add(new Point(j,i));
131+
continue;
132+
}
133+
134+
Queue<Point> q = new LinkedList<>();
135+
Set<Point> set = new HashSet<>();
136+
137+
Point start = new Point(j,i);
138+
boolean isInside = true;
139+
q.add(start);
140+
set.add(start);
141+
142+
while(!q.isEmpty()) {
143+
Point p = q.poll();
144+
145+
for (int k = 0; k <4 ; k++) {
146+
int ny = p.y + dy[k];
147+
int nx = p.x + dx[k];
148+
149+
if ( ny < 0 || nx < 0 || ny >= height || nx >= width) {
150+
// 밖으로 벗어남 -> 치즈안이 아님
151+
isInside = false;
152+
continue;
153+
}
154+
155+
if (visited[ny][nx] || arr[ny][nx] == CHEESE) continue;
156+
157+
Point np = new Point(nx, ny);
158+
q.add(np);
159+
set.add(np);
160+
visited[ny][nx] = true;
161+
162+
}
163+
}
164+
165+
for (Point p:set) {
166+
arr[p.y][p.x] = (isInside)?2:0;
167+
}
168+
169+
}
170+
}
171+
}
172+
173+
174+
175+
176+
public static void print() {
177+
System.out.println(ans);
178+
179+
}
180+
}
181+
```

0 commit comments

Comments
 (0)