Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

์„ค๋ช…

๊ฐ€๊ณ„๋„์—์„œ ๋‘ ์‚ฌ๋žŒ๊ฐ„์˜ ์ดŒ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ. ์ฒซ๋ฒˆ์งธ ์˜ˆ์ œ๋กœ ์ฃผ์–ด์ง„ ์ดŒ์ˆ˜์— ๋Œ€ํ•ด 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ‘œ์‹œํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทธ๋ฆผ์ด ๋œ๋‹ค. img_3.png

ํ’€์ด (DFS)

๊ด€๊ณ„๋ฅผ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ  ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์— ๋Œ€ํ•ด์„œ๋Š” visited ๋ฐฐ์—ด์˜ ๊ฐ’์„ true ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค. ํ•œ๋ฒˆ ํ˜ธ์ถœ๋ ๋•Œ ๋งˆ๋‹ค depth ๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ์ด ๊ฐ’์ด ์—ฌ๊ธฐ์„  ์ดŒ์ˆ˜๊ฐ€ ๋œ๋‹ค. 7๋ฒˆ ์‚ฌ๋žŒ๊ณผ 3๋ฒˆ ์‚ฌ๋žŒ์˜ ์ดŒ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค๋ฉด 7 -> 2 -> 1 -> 3 ์ˆœ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด ๋œ๋‹ค. 3๋ฒˆ ์‚ฌ๋žŒ๊นŒ์ง€ ํƒ์ƒ‰์ด ์™„๋ฃŒ๋˜๋ฉด depth ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class Main {

  static boolean[][] graph;
  static boolean[] visited;
  static int answer = -1;
  static int n, m;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    n = Integer.parseInt(br.readLine());

    graph = new boolean[n + 1][n + 1];
    visited = new boolean[n + 1];

    StringTokenizer st = new StringTokenizer(br.readLine());
    int p1 = Integer.parseInt(st.nextToken());
    int p2 = Integer.parseInt(st.nextToken());

    m = Integer.parseInt(br.readLine());
    int x, y;
    for (int i = 0; i < m; i++) {
      StringTokenizer kinship = new StringTokenizer(br.readLine());
      x = Integer.parseInt(kinship.nextToken());
      y = Integer.parseInt(kinship.nextToken());
      // ์„œ๋กœ ๊ด€๊ณ„๋ฅผ ๋งบ์–ด์ค€๋‹ค
      graph[x][y] = true;
      graph[y][x] = true;
    }

    dfs(p1, p2, 0);
    System.out.println(answer);
  }

  private static void dfs(int p1, int p2, int depth) {
    if (p1 == p2) {
      answer = depth;
      return;
    }
    visited[p1] = true;

    for (int j = 1; j <= n; j++) {
      if (!visited[j] && graph[p1][j]) { // ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๊ณ , ํ•ด๋‹น ๋…ธ๋“œ์˜ graph ๊ฐ€ true ์ด๋ฉด (์นœ์ฒ™๊ด€๊ณ„์ด๋ฉด) ๋ฐฉ๋ฌธ
        dfs(j, p2, depth + 1);
      }
    }
  }
}

ํ’€์ด (BFS)

dfs ๊ฐ€ ์ตœ์ดˆ ์‹œ์ž‘์ง€์  ๋ถ€ํ„ฐ ์ด์–ด์ง„ ๋…ธ๋“œ๋ฅผ ๋๊นŒ์ง€ ์žฌ๊ท€์ ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ฐพ๋Š” ๋ฐฉ์‹์ด๋ผ๋ฉด, bfs ๋Š” ์‹œ์ž‘ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋“ค์„ ๋ชจ๋‘ Queue ์— ๋‹ด์•˜๋‹ค๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ์ฐพ๋Š” ๋ฐฉ์‹์ด๋‹ค. bfs ํ˜ธ์ถœ ์‹œ ์‹œ์ž‘๋…ธ๋“œ์™€ ํ•จ๊ป˜ depth ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class Main {

  static boolean[][] graph;
  static boolean[] visited;
  static int answer = -1;
  static int n, m;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    n = Integer.parseInt(br.readLine());

    graph = new boolean[n + 1][n + 1];
    visited = new boolean[n + 1];

    StringTokenizer st = new StringTokenizer(br.readLine());
    int p1 = Integer.parseInt(st.nextToken());
    int p2 = Integer.parseInt(st.nextToken());

    m = Integer.parseInt(br.readLine());
    int x, y;
    for (int i = 0; i < m; i++) {
      StringTokenizer kinship = new StringTokenizer(br.readLine());
      x = Integer.parseInt(kinship.nextToken());
      y = Integer.parseInt(kinship.nextToken());
      graph[x][y] = true;
      graph[y][x] = true;
    }

    System.out.println(bfs(p1, p2));
  }

  private static int bfs(int p1, int p2) {
    LinkedList<int[]> queue = new LinkedList<>();
    queue.add(new int[] {p1, 0});
    visited[p1] = true;

    while (!queue.isEmpty()) {
      int[] current = queue.poll();
      int node = current[0];
      int depth = current[1];

      if (node == p2) {
        return depth;
      }

      for (int i = 1; i <= n; i++) {
        if (!visited[i] && graph[node][i]) {
          visited[i] = true;
          queue.add(new int[] {i, depth + 1});
        }
      }
    }
    return answer;
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ