-
Notifications
You must be signed in to change notification settings - Fork 3
[Solved] PRG_레벨2_석유 시추 #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShineCorine
wants to merge
2
commits into
Kernel360-4cell:main
Choose a base branch
from
ShineCorine:week17/tue2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분 좀 궁금한게..
제가 안됐던 이유는 석유가 있는 모든 땅에서 dfs를 돌렸기때문에 시초가 났단말이죠?
근데 이것도 결국엔 석유가 있는 모든 땅에서 bfs를 돌려서 똑같은거아닌가요..? 진짜 너무답답함..
There was a problem hiding this comment.
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으로 선언해서 같은 방법으로 수행했을 것 같아요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아..그러네요 어떤건지 좀 느낌이 올것같습니다 답변 감사합니다!! 오늘다시해본다 ㅠ