Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Week 17/PRG_레벨1_석유 시추/Jongmin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.util.*;

class Solution {
public static int TC; // Total colums
public static int TR; // Totla rows
public static boolean[][] visited;
public static int[][] directions = {{-1,0}, {0, -1}, {1, 0}, {0, 1}};
public int solution(int[][] land) {
int answer = 0;
int[] oils;
TC = land[0].length;
TR = land.length;
visited = new boolean[TR][TC];

oils = new int[TC];
for(int i=0; i< TC; i++){
oils[i] = 0;
}

for(int c=0; c<TC; c++){
for(int r=0; r<TR; r++)
if(land[r][c] == 1 &&!visited[r][c]){
List<Integer> rv = getOilSize(r, c, land);
for(int i=rv.get(1); i<=rv.get(2); i++){
oils[i]+=rv.get(0);
}
Comment on lines +22 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분 좀 궁금한게..
제가 안됐던 이유는 석유가 있는 모든 땅에서 dfs를 돌렸기때문에 시초가 났단말이죠?
근데 이것도 결국엔 석유가 있는 모든 땅에서 bfs를 돌려서 똑같은거아닌가요..? 진짜 너무답답함..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 visited가 초기화가 되지 않아요. 따라서 앞에서 true로 표시된 땅은 다시 탐색을 하지 않게 했어요. 그래서 석유 덩어리를 계산하는데 드는 시간이 최대 mxn이 되는데요, 만약에 초기화를 했다면 저기에다가 매 컬럼 에서 매 로우를 탐색할 때마다 포함된 석유를 계산하게 돼요. 만약에
000000
110000
110000
000000
이런식으로 매장이 되어 있다면 저 4개짜리 덩어리를 총 4회 탐색하게 되는 것 같아요 여기서는 dfs나 bfs는 크게 상관이 없는 것 같아요. 만약에 제가 재귀함수를 사용했다면 isVisited를 메인 밖에서 static으로 선언해서 같은 방법으로 수행했을 것 같아요.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아..그러네요 어떤건지 좀 느낌이 올것같습니다 답변 감사합니다!! 오늘다시해본다 ㅠ

}
}
answer = oils[0];
for(int i=0; i< TC; i++){
answer = Math.max(answer, oils[i]);
}

return answer;
}


static List<Integer> getOilSize(int r, int c, int[][] land){

int size = 1;
int minC = c;
int maxC = c;

Deque<Point> queue = new ArrayDeque<>();
queue.add(new Point(r, c));
visited[r][c] = true;

while(!queue.isEmpty()){

Point point = queue.removeFirst();

int cc = point.getC();
int cr = point.getR();

for(int i=0; i<4; i++){
int dc = directions[i][1];
int dr = directions[i][0];

int nc = cc + dc;
int nr = cr + dr;

if(isValidCooridate(nr, nc)&& land[nr][nc]==1 && !visited[nr][nc]){
visited[nr][nc] = true;
size++;
queue.add(new Point(nr, nc));
minC = Math.min(minC, nc);
maxC = Math.max(maxC, nc);
}

}// end for


}// end while


return List.of(size, minC, maxC);

}

private static boolean isValidCooridate(int r, int c){
return r>=0 && r<TR && c>=0 && c< TC;
}
static class Point{
private int r;
private int c;
public Point(int r, int c){
this.r = r;
this.c = c;
}
public int getC(){
return this.c;
}
public int getR(){
return this.r;
}
}
}