Skip to content

Commit 4bd4d0e

Browse files
committed
[2020kakao(intern)-p5) 동굴 탐험
1 parent 75dc0b9 commit 4bd4d0e

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,4 @@
519519
| 02 | | [수식 최대화](./src/_2020_카카오_인턴십/P2) | 구현 |
520520
| 03 | | [보석 쇼핑](./src/_2020_카카오_인턴십/P3) | 2-pointer |
521521
| 04 | | [경주로 건설](./src/_2020_카카오_인턴십/P4) | dfs + dp |
522+
| 05 | | [동굴 탐험](./src/_2020_카카오_인턴십/P5) | graph (cycle check : stack overflow) |
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package _2020_카카오_인턴십.P5;
2+
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
Solution sol = new Solution();
9+
System.out.println(sol.solution(9, new int[][]{{0,1},{0,3},{0,7},{8,1},{3,6},{1,2},{4,7},{7,5}},
10+
new int[][]{{8,5},{6,7},{4,1}}));
11+
System.out.println(sol.solution(9, new int[][]{{8,1},{0,1},{1,2},{0,7},{4,7},{0,3},{7,5},{3,6}},
12+
new int[][]{{4,1}, {5, 2}}));
13+
System.out.println(sol.solution(9, new int[][]{{0,1},{0,3},{0,7},{8,1},{3,6},{1,2},{4,7},{7,5}},
14+
new int[][]{{4,1},{8,7},{6,5}}));
15+
}
16+
}
17+
18+
class Solution {
19+
20+
ArrayList<Integer>[] paths, graph;
21+
boolean[] visited, finished;
22+
boolean hasCycle = false;
23+
24+
public boolean solution(int n, int[][] path, int[][] order) {
25+
26+
paths = new ArrayList[n];
27+
graph = new ArrayList[n];
28+
for (int i = 0; i < n; i++) {
29+
graph[i] = new ArrayList<>();
30+
paths[i] = new ArrayList<>();
31+
}
32+
33+
for (int[] p : path) {
34+
paths[p[0]].add(p[1]);
35+
paths[p[1]].add(p[0]);
36+
}
37+
38+
visited = new boolean[n];
39+
Queue<Integer> q = new LinkedList<>();
40+
41+
q.offer(0);
42+
visited[0] = true;
43+
while (!q.isEmpty()) {
44+
int from = q.poll();
45+
for (int to : paths[from]) {
46+
if (visited[to]) continue;
47+
visited[to] = true;
48+
graph[from].add(to);
49+
q.offer(to);
50+
}
51+
}
52+
53+
for (int[] o : order) graph[o[0]].add(o[1]);
54+
55+
visited = new boolean[n];
56+
finished = new boolean[n];
57+
dfs(0);
58+
return !hasCycle;
59+
}
60+
61+
void dfs(int node) {
62+
visited[node] = true;
63+
for (int next : graph[node]) {
64+
if (!visited[next]) dfs(next);
65+
else if (!finished[next]) {
66+
hasCycle = true;
67+
return;
68+
}
69+
}
70+
finished[node] = true;
71+
}
72+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## [2020 카카오 인턴십] 동굴 탐험
2+
3+
![image](https://user-images.githubusercontent.com/22045163/116604124-b4350a80-a968-11eb-906c-5dab06c5d19f.png)
4+
5+
### Stack Overflow 이슈
6+
7+
처음에 문제 풀이가 떠오르지 않아 해설을 참고한 후 구현(**양방향 그래프 -> 단방향 그래프 변환 -> 사이클 체크**)하였고, 돌렸는데 효율성 점수에서 마지막 2개가 `런타임 에러`가 나는 것이다. 이 문제로 3시간을 헤맸다. 휴.......
8+
9+
입력값의 크기가 매우 크니 dfs 호출을 하는만큼 스택 오버플로우가 나는 것이라면, Stack을 활용해 iterative하게 구현하는 방법이 있다. 하지만 그 방법이 떠오르지 않아서 몇 번을 코드를 짜고 지웠다. (시간날 때 인터넷을 더 찾아봐야겠다.) 그리고 어이가 없던 것은 어쨌든 호출 횟수를 줄이니 풀렸던 것..
10+
11+
```java
12+
void dfs(int node) {
13+
visited[node] = true;
14+
for (int next : graph[node]) {
15+
if (!visited[next]) {
16+
for (int nnext : graph[next]) {
17+
if (!visited[nnext]) dfs(nnext);
18+
else if (!finished[nnext]) {
19+
hasCycle = true;
20+
return;
21+
}
22+
}
23+
} else if (!finished[next]) {
24+
hasCycle = true;
25+
return;
26+
}
27+
}
28+
finished[node] = true;
29+
}
30+
```
31+
32+
내 코드에는 이렇게 올려놓지 않겠다 !!!

0 commit comments

Comments
 (0)