diff --git "a/Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" "b/Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" new file mode 100644 index 0000000..cae7b25 --- /dev/null +++ "b/Programmers/\353\266\200\353\214\200\353\263\265\352\267\200/\353\266\200\353\214\200\353\263\265\352\267\200_\354\265\234\354\247\200\354\210\230.java" @@ -0,0 +1,35 @@ +import java.util.*; + +class Solution { + public int[] solution(int n, int[][] roads, int[] sources, int destination) { + int[] answer = new int[sources.length]; + ArrayList[] map = new ArrayList[n+1]; + for (int i = 1; i < n+1; i++) { + map[i] = new ArrayList<>(); + } + for (int[] road : roads) { + map[road[0]].add(road[1]); + map[road[1]].add(road[0]); + } + + ArrayDeque que = new ArrayDeque<>(); + int[] visit = new int[n+1]; + Arrays.fill(visit, -1); + visit[destination] = 0; + que.add(destination); + while (!que.isEmpty()) { + int now = que.poll(); + for (int i : map[now]) { + if (visit[i] != -1) continue; + visit[i] = visit[now]+1; + que.add(i); + } + } + + for (int i = 0; i < sources.length; i++) { + answer[i] = visit[sources[i]]; + } + + return answer; + } +} \ No newline at end of file