Skip to content

Commit ad42be4

Browse files
committed
nsun9505 solved 동굴탐험
1 parent 76cda8f commit ad42be4

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2020 카카오 인턴십] 동굴 탐험 - JAVA
2+
3+
## 분류
4+
> BFS
5+
6+
## 코드
7+
```java
8+
import java.util.*;
9+
10+
public class Solution {
11+
public boolean solution(int n, int[][] path, int[][] order) {
12+
ArrayList<Integer>[] graph = new ArrayList[n];
13+
for(int i=0; i<graph.length; i++)
14+
graph[i] = new ArrayList<>();
15+
16+
HashMap<Integer, Integer> orders = new HashMap<>();
17+
HashMap<Integer, Integer> readyMap = new HashMap<>();
18+
for(int i=0; i<order.length; i++)
19+
orders.put(order[i][1], order[i][0]);
20+
21+
if(orders.containsKey(0) && orders.get(0) != 0)
22+
return false;
23+
24+
for(int i=0; i<path.length; i++){
25+
graph[path[i][0]].add(path[i][1]);
26+
graph[path[i][1]].add(path[i][0]);
27+
}
28+
29+
Queue<Integer> queue = new LinkedList<>();
30+
boolean[] visited = new boolean[n];
31+
visited[0] = true;
32+
queue.offer(0);
33+
34+
while(!queue.isEmpty()){
35+
int cur = queue.poll();
36+
37+
if(readyMap.containsKey(cur)){
38+
int next = readyMap.get(cur);
39+
visited[next] = true;
40+
queue.offer(next);
41+
}
42+
43+
for(int next : graph[cur]){
44+
if(visited[next])
45+
continue;
46+
47+
if(orders.containsKey(next)){
48+
int prev = orders.get(next);
49+
if(!visited[prev]) {
50+
readyMap.put(prev, next);
51+
continue;
52+
}
53+
54+
visited[next] = true;
55+
queue.offer(next);
56+
} else {
57+
visited[next] = true;
58+
queue.offer(next);
59+
}
60+
}
61+
}
62+
63+
for(int i=0; i<visited.length; i++)
64+
if(!visited[i])
65+
return false;
66+
return true;
67+
}
68+
}
69+
```
70+
71+
## 문제 풀이
72+
약간 위상정렬 같은 느낌이였지만, order가 인접한 방이 아닐 수도 있으므로 위상정렬로 푸는 것은 아닌거 같습니다.
73+
74+
그래서 BFS로 방문할 수 있는 방들은 무조건 방문을 합니다!
75+
76+
그러다가 아직 조건 충족이 되지 않아 방문을 하지 못하는 애들은 readyMap에 넣어서 방문을 하려고 헀지만! 조건이 충족되지 않아 방문하지 못한 방으로 남겨둡니다.
77+
78+
queue에서 pop한 방이 readyMap에 있는 방 중에 어떤 방의 먼저 방문해야 하는 방이라면 readyMap에서 현재 방문한 방을 키 값으로 현재 방을 방문하였을 때 방문할 수 있는
79+
80+
방을 queue에 넣어줍니다!
81+
82+
이렇게 진행해서 모든 방을 방문했다면 true를 리턴하고, 아직 방문하지 못한 방이 있다면 false를 리턴합니다.
83+
84+
## 후기
85+
푸는 방법이 머리에 남아 있어요.. 어떡하죠..
86+
87+
여기서 어떻게 하면 될까 하다가 아 이렇게 하면 되겠다가 예전에 했던 방식이랑 비슷하네유..
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public boolean solution(int n, int[][] path, int[][] order) {
5+
ArrayList<Integer>[] graph = new ArrayList[n];
6+
for(int i=0; i<graph.length; i++)
7+
graph[i] = new ArrayList<>();
8+
9+
HashMap<Integer, Integer> orders = new HashMap<>();
10+
HashMap<Integer, Integer> readyMap = new HashMap<>();
11+
for(int i=0; i<order.length; i++)
12+
orders.put(order[i][1], order[i][0]);
13+
14+
if(orders.containsKey(0) && orders.get(0) != 0)
15+
return false;
16+
17+
for(int i=0; i<path.length; i++){
18+
graph[path[i][0]].add(path[i][1]);
19+
graph[path[i][1]].add(path[i][0]);
20+
}
21+
22+
Queue<Integer> queue = new LinkedList<>();
23+
boolean[] visited = new boolean[n];
24+
visited[0] = true;
25+
queue.offer(0);
26+
27+
while(!queue.isEmpty()){
28+
int cur = queue.poll();
29+
30+
if(readyMap.containsKey(cur)){
31+
int next = readyMap.get(cur);
32+
visited[next] = true;
33+
queue.offer(next);
34+
}
35+
36+
for(int next : graph[cur]){
37+
if(visited[next])
38+
continue;
39+
40+
if(orders.containsKey(next)){
41+
int prev = orders.get(next);
42+
if(!visited[prev]) {
43+
readyMap.put(prev, next);
44+
continue;
45+
}
46+
47+
visited[next] = true;
48+
queue.offer(next);
49+
} else {
50+
visited[next] = true;
51+
queue.offer(next);
52+
}
53+
}
54+
}
55+
56+
for(int i=0; i<visited.length; i++)
57+
if(!visited[i])
58+
return false;
59+
return true;
60+
}
61+
}

0 commit comments

Comments
 (0)