Skip to content

Commit 3484b53

Browse files
committed
[D4] Title: [S/W 문제해결 기본] 4일차 - 길찾기, Time: 198 ms, Memory: 35,072 KB -BaekjoonHub
1 parent a765326 commit 3484b53

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

SWEA/D4/1219. [S/W 문제해결 기본] 4일차 - 길찾기/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
### 성능 요약
66

7-
메모리: 34,944 KB, 시간: 182 ms, 코드길이: 1,786 Bytes
7+
메모리: 35,072 KB, 시간: 198 ms, 코드길이: 2,526 Bytes
88

99
### 제출 일자
1010

11-
2025-10-31 17:28
11+
2025-10-31 17:36
1212

1313

1414

SWEA/D4/1219. [S/W 문제해결 기본] 4일차 - 길찾기/[S/W 문제해결 기본] 4일차 - 길찾기.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import java.util.*;
23

34
class Solution
@@ -40,11 +41,12 @@ private static int getAnswer(int [] route) {
4041
map.get(from).add(to);
4142

4243
}
43-
44+
4445
Set<Integer> visited = new HashSet<>();
45-
return dfs(0, 99, map, visited) ? 1 : 0;
46+
47+
return bfs(0, 99, map, visited) ? 1 : 0;
4648
}
47-
49+
4850
private static boolean dfs(
4951
int start, int end, HashMap<Integer, List<Integer>> map, Set<Integer> visited) {
5052
if (start == end) {
@@ -58,7 +60,7 @@ private static boolean dfs(
5860
}
5961

6062
for (int next : map.get(start)) {
61-
if (!visited.contains(next)) { //
63+
if (!visited.contains(next)) { //
6264
if (dfs(next, end, map, visited)) {
6365
return true;
6466
}
@@ -67,4 +69,33 @@ private static boolean dfs(
6769

6870
return false;
6971
}
72+
73+
private static boolean bfs(
74+
int start, int end, HashMap<Integer, List<Integer>> map, Set<Integer> visited
75+
) {
76+
Queue<Integer> queue = new LinkedList<>();
77+
78+
queue.add(start);
79+
80+
while(!queue.isEmpty()) {
81+
int current = queue.poll();
82+
83+
if (current == end) {
84+
return true;
85+
}
86+
87+
if (!map.containsKey(current)) {
88+
continue;
89+
}
90+
91+
for (int next : map.get(current)) {
92+
if (!visited.contains(next)) {
93+
queue.add(next);
94+
visited.add(next);
95+
}
96+
}
97+
}
98+
99+
return false;
100+
}
70101
}

0 commit comments

Comments
 (0)