Skip to content
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

김지훈 : boj 18352 특정 거리의 도시 찾기 #246

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hoonti06
Copy link

@hoonti06 hoonti06 commented May 4, 2021

🅰 설계

  • 해당 정점의 depth를 저장하는 배열 (-1로 초기화)

    static int[] visited;
  • bfs로 주어진 K depth 까지만 수행하면서 각 정점의 depth를 저장

    Queue<Integer> q = new ArrayDeque<>();
    q.offer(X);
    visited[X] = 0;
    
    int cnt = 1;
    while (!q.isEmpty() && cnt <= K) {
      for (int qs = 0, qSize = q.size(); qs < qSize; qs++) {
        int cur = q.poll();
    
        for (int i = 0; i < edge[cur].size(); i++) {
          int next = edge[cur].get(i);
          if (visited[next] != -1) continue;
          visited[next] = cnt;
          q.offer(next);
        }
      }
      cnt++;
    }
  • K depth에 해당하는 정점들을 StringBuilder에 저장

    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= N; i++)
      if (visited[i] == K)
        sb.append(i).append('\n');
    return sb.length() == 0 ? "-1" : sb.toString();

✅ 후기

  • dijkstra로 풀었을 때는 시간이 1296ms 소요되고, bfs로 풀었을 때는 시간이 1096ms 소요됐는데 의미 있는 시간 차이인지는 모르겠습니다.

@hoonti06 hoonti06 added BFS/DFS 그래프 탐색 BOJ Baekjoon 다익스트라 Dijkstra's labels May 4, 2021
@hoonti06 hoonti06 self-assigned this May 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BFS/DFS 그래프 탐색 BOJ Baekjoon 다익스트라 Dijkstra's
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant