Skip to content

Commit e802424

Browse files
committed
Programmers_250136 : 석유 시추
1 parent cd53fcd commit e802424

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
private int[][] visited;
5+
private int oilNum;
6+
private Map<Integer, Integer> oilMap;
7+
private int[][] land;
8+
9+
public int solution(int[][] _land) {
10+
int answer = 0;
11+
land = _land;
12+
visited = new int[land.length][land[0].length];
13+
oilNum = 1;
14+
oilMap = new HashMap<>();
15+
for(int i=0; i<land.length; i++) {
16+
for(int j=0; j<land[0].length; j++) {
17+
if(land[i][j] == 1 && visited[i][j] == 0) {
18+
bfs(i, j);
19+
}
20+
}
21+
}
22+
for(int i=0; i<land[0].length; i++) {
23+
boolean[] check = new boolean[oilNum];
24+
int count = 0;
25+
for(int j=0; j<land.length; j++) {
26+
int num = visited[j][i];
27+
if(num > 0 && !check[num]) {
28+
check[num] = true;
29+
count += oilMap.get(num);
30+
}
31+
}
32+
answer = Math.max(answer, count);
33+
}
34+
return answer;
35+
}
36+
37+
public void bfs(int startH, int startW) {
38+
int[] dh = {0, 1, 0, -1};
39+
int[] dw = {-1, 0, 1, 0};
40+
Queue<int[]> q = new LinkedList<>();
41+
q.offer(new int[]{startH, startW});
42+
visited[startH][startW] = oilNum;
43+
int count = 1;
44+
while(!q.isEmpty()) {
45+
int h = q.peek()[0];
46+
int w = q.peek()[1];
47+
q.poll();
48+
for(int i=0; i<4; i++) {
49+
int nextH = h + dh[i];
50+
int nextW = w + dw[i];
51+
if(nextH >= 0 && nextH < land.length && nextW >= 0 && nextW < land[0].length) {
52+
if(land[nextH][nextW] == 1 && visited[nextH][nextW] == 0) {
53+
visited[nextH][nextW] = oilNum;
54+
q.offer(new int[]{nextH, nextW});
55+
count++;
56+
}
57+
}
58+
}
59+
}
60+
oilMap.put(oilNum, count);
61+
oilNum++;
62+
}
63+
}

0 commit comments

Comments
 (0)