Skip to content

Commit c8fa7f0

Browse files
committed
add: PCCP 기출문제 2번 - 석유 시추
1 parent 578eb56 commit c8fa7f0

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.*;
2+
import java.util.stream.*;
3+
4+
class Solution {
5+
static List<Integer> list = new ArrayList<>();
6+
static int current = 0;
7+
static int[][] visited;
8+
static int[][] board;
9+
static int[][] d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
10+
static int n, m;
11+
12+
public int solution(int[][] land) {
13+
board = land;
14+
n = land.length;
15+
m = land[0].length;
16+
visited = new int[n][m];
17+
for(int i = 0; i < n; i++) {
18+
Arrays.fill(visited[i], -1);
19+
}
20+
for(int i = 0; i < n; i++) {
21+
for(int j = 0; j < m; j++) {
22+
if(land[i][j] == 1 && visited[i][j] == -1) {
23+
list.add(bfs(i, j));
24+
current++;
25+
}
26+
}
27+
}
28+
29+
int answer = 0;
30+
for(int j = 0; j < m; j++) {
31+
final int row = j;
32+
int value = IntStream.range(0, n)
33+
.map(i -> visited[i][row])
34+
.filter(index -> index != -1)
35+
.distinct()
36+
.map(a -> list.get(a))
37+
.sum();
38+
answer = Math.max(answer, value);
39+
}
40+
return answer;
41+
}
42+
43+
int bfs(int i, int j) {
44+
int sum = 0;
45+
Deque<int[]> que= new ArrayDeque<>();
46+
que.offer(new int[]{i, j});
47+
visited[i][j] = current;
48+
while(!que.isEmpty()) {
49+
sum++;
50+
int[] cur = que.poll();
51+
52+
for(int[] next : d) {
53+
int ny = next[0] + cur[0];
54+
int nx = next[1] + cur[1];
55+
if(!isValidRange(ny, nx) || visited[ny][nx] != -1 || board[ny][nx] != 1) continue;
56+
visited[ny][nx] = current;
57+
que.offer(new int[]{ny, nx});
58+
}
59+
}
60+
return sum;
61+
}
62+
63+
private boolean isValidRange(int r, int c) {
64+
return r >= 0 && c >= 0 && r < n && c < m;
65+
}
66+
}

0 commit comments

Comments
 (0)