-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2636_์น์ฆ.java
More file actions
97 lines (84 loc) ยท 3.8 KB
/
2636_์น์ฆ.java
File metadata and controls
97 lines (84 loc) ยท 3.8 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int R, C, cnt, ans, cheeseCnt;
static int[][] map;
static int[] dx = new int[]{1,-1,0,0}; //๋ฐฉํฅ๋ฒกํฐ
static int[] dy = new int[]{0,0,1,-1};
static List<int[]> list = new ArrayList<>();
static List<Integer> cntList = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
map = new int[R][C];
for(int i=0; i<R; i++) { //๋งต ์
๋ ฅ
st = new StringTokenizer(br.readLine());
for(int j=0; j<C; j++) {
int idx = Integer.parseInt(st.nextToken());
if(idx == 1) list.add(new int[]{i,j}); //์น์ฆ๋ง ๋ฐ๋ก ๋ฆฌ์คํธ์ ๊ด๋ฆฌ
map[i][j] = idx;
}
}
if(!list.isEmpty()) cheeseCnt = list.size(); //์น์ฆ๊ฐ 1๊ฐ ์ด์ ์์ ๊ฒฝ์ฐ ์น์ฆ๊ฐ์ ์ ์ฅ
while(!list.isEmpty()) {
bfs(); //0,0๊ธฐ์ค ์น์ฆ ๋ฐ๊นฅ ๊ณต๊ธฐ๋ค์ ๋ํด ๊ฐ ๊ฐฑ์
int[] deleteList = new int[list.size()]; //์ ๊ฑฐํ ์น์ฆ๋ฅผ ๊ด๋ฆฌํ ๋ฐฐ์ด
for(int i=0; i<list.size(); i++) { //์ฐพ์ ๋ ๋ง๋ค ๊ฐ ๋ณ๊ฒฝํ๋ฉด ์๋จ.
int[] idx = list.get(i);
if(!check(idx[0], idx[1])) { //์ค์ธ๊ณต๊ธฐ์ ๋ง๋ฟ์ ์๋ ์น์ฆ๋ค์ ๋ํด
deleteList[i] = 1;
}
}
for(int i=0; i<deleteList.length; i++) { //์ผ๊ด๋ณ๊ฒฝ
if(deleteList[i] == 1) { //์ ๊ฑฐํ ์น์ฆ๋ผ๋ฉด
int[] idx = list.get(i);
map[idx[0]][idx[1]] = 2; //์ค์ธ๊ณต๊ธฐ์ ๊ฐ์ ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
}
}
list = new ArrayList<>();
cnt = 0; //๊ฐ ์ด๊ธฐํ
for(int i=0; i<R; i++) {
for(int j=0; j<C; j++) {
if(map[i][j] == 1) {
cnt++;
list.add(new int[]{i,j}); //์น์ฆ์ ๋ํ ์๋ก์ด ๋ฆฌ์คํธ ๊ด๋ฆฌ
}
}
}
cntList.add(cnt); //์น์ฆ๊ฐ์ ๊ด๋ฆฌ
ans++; //๊ฒฐ๊ณผ์๊ฐ ์ฆ๊ฐ
}
//2์ด ์ด์ ๊ฑธ๋ฆด ๊ฒฝ์ฐ ์ด์ ์น์ฆ๊ฐ์๋ฅผ ์ฐธ์กฐ, 1์ด์ ๋์ด ๋ฌ๋ค๋ฉด ์ฒ์์ ์ ์ฅํด๋์ ๊ฐ ์ถ๋ ฅ
if(cntList.size() > 1) cheeseCnt = cntList.get(cntList.size() - 2);
System.out.println(ans + "\n" + cheeseCnt); //๊ฑธ๋ฆฐ ์๊ฐ ๋ฐ ์ด์ ์น์ฆ๊ฐ์ ์ถ๋ ฅ
}
private static boolean check(int x, int y) { //์น์ฆ์ขํ์ ๋ํด ํด๋น ์น์ฆ๊ฐ ์ ๊ฑฐ๋๋์ง ์ฒดํฌํ๋ ๋ฉ์๋
for(int i=0; i<4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(map[nx][ny] == 2) return false; //์ฃผ๋ณ 4๋ฐฉ ์ค ์ค์ธ๊ณต๊ธฐ๊ฐ ํ๊ฐ๋ผ๋ ๋ง๋ฟ์ ์๋ค๋ฉด
}
return true;
}
private static void bfs() { //์ค์ธ ๊ณต๊ธฐ์ ๋ํ ๊ฐ ๋ณ๊ฒฝ์ ์ํ ๋๋นํ์
Queue<int[]> queue = new ArrayDeque<>();
queue.offer(new int[]{0,0});
boolean[][] v = new boolean[R][C];
v[0][0] = true;
while(!queue.isEmpty()) {
int[] idx = queue.poll();
for(int i=0; i<4; i++) {
int nx = idx[0] + dx[i];
int ny = idx[1] + dy[i];
if(nx<0 || ny<0 || nx>=R || ny>=C || map[nx][ny] == 1 || v[nx][ny]) continue; //์ ์ฒ๋ฆฌ : ๋งต ๋ฐ, ์น์ฆ, ๋ฐฉ๋ฌธ์ฒดํฌ
map[nx][ny] = 2;
v[nx][ny] = true;
queue.offer(new int[]{nx,ny});
}
}
}
}