Algorithm

๋ฐฑ์ค€ 2667 | DFS, ๋‹จ์ง€ ๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ

osean 2020. 11. 2. 17:23

๊ทธ๋ž˜ํ”„, DFS, BFS ๋ณต์Šตํ•˜๊ธฐ

 

4-1) Graph, DFS, BFS

ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

www.notion.so

์–ผ์Œ๊ณผ ๋ฏธ๋กœ ๊ทธ ์‚ฌ์ด ์–ด๋”˜๊ฐ€

์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ์ ‘ํ–ˆ์„ ๋•Œ, ์ธ์ ‘ ํ–‰๋ ฌ๋กœ ๋งŒ๋“ค์–ด์ง„ ํ…Œ์ด๋ธ”์„ ๋ณด๊ณ 

"BFS๋กœ ํ’€์–ด์•ผ ํ•˜๋‚˜?"

๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ตฌ์กฐ๋Š” ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š์ง€๋งŒ BFS(๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰)์ด ์žฌ๊ท€๋‚˜ Stack ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , Queue ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ณต๋ถ€ ํ–ˆ๋˜ ๋‚ด์šฉ์„ ํ†บ์•„๋ณด๋ฉฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

๋„์ „! BFS!

package ์‹ฌ์„ฑํ—Œ.์•Œ๊ณ ๋ฆฌ์ฆ˜_5์ฃผ์ฐจ;

import java.io.*;
import java.util.*;

public class ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ_BFS {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int n;
    static int length = 0;
    static int cnt = 0;
    static int[][] arr;

    public static void main(String[] args) throws Exception {
        n = Integer.parseInt(br.readLine());
        arr = new int[n][n];
        StringTokenizer[] st = new StringTokenizer[n];
        for (int i = 0; i < n; i++) {
            st[i] = new StringTokenizer(br.readLine());
            char[] tmp = st[i].nextToken().toCharArray();
            for (int k = 0; k < tmp.length; k++) {
                arr[i][k] = tmp[k] - '0';
            }
        }
        int[] xy = { 0, 0 };
        bfs(xy);
        for (int i = 0; i < arr.length; i++) {
            System.out.print("arr[" + i + "] = ");
            for (int k = 0; k < arr[i].length; k++) {
                System.out.print(arr[i][k]);
            }
            System.out.println();
        }
    }

    public static void bfs(int[] xy) {

        Queue<int[]> queue = new LinkedList<int[]>();
        queue.offer(xy);

        while (!queue.isEmpty()) {
            xy = queue.poll();
            int[][] nxy = { { -1, 1, 0, 0 }, { 0, 0, -1, 1 } };

            for (int i = 0; i < 4; i++) {
                nxy[0][i] = xy[0] + nxy[0][i];
                nxy[1][i] = xy[1] + nxy[1][i];
                int[] tmp = { nxy[0][i], nxy[1][i] };

                if (nxy[0][i] < 0 || nxy[1][i] < 0 || nxy[0][i] >= n || nxy[1][i] >= n)
                    continue;

                System.out.println("arr[" + nxy[0][i] + "][" + nxy[1][i] + "] = " + arr[nxy[0][i]][nxy[1][i]]);

                if (arr[nxy[0][i]][nxy[1][i]] == 1) {
                    arr[nxy[0][i]][nxy[1][i]] = 5;
                    queue.offer(tmp);
                    cnt++;
                }
            }
        }
    }
}

๊ฒฐ๊ณผ๋ฅผ ๋งํ•˜์ž๋ฉด ์‹คํŒจ๋‹ค. ๋จผ์ € ๊ฒฐ๊ณผ ์ถœ๋ ฅ์„ ํ™•์ธ ํ•ด๋ณด์ž.

์‹คํŒจํ•œ ์ด์œ ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. Queue์— ์Œ“์ธ ๋…ธ๋“œ์˜ ์ขŒํ‘œ ์ค‘ ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ์ค€์˜ ์ƒํ•˜์ขŒ์šฐ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ , ํƒ์ƒ‰ํ•œ ์ขŒํ‘œ์˜ ๋…ธ๋“œ๊ฐ€ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด Queue์— ์Œ“์ด์ง€ ์•Š๋Š”๋‹ค.
  2. ์ฆ‰, Queue์˜ ๋งˆ์ง€๋ง‰ ์ขŒํ‘œ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ ํ–ˆ์„ ๋•Œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด Queue์—๋Š” ์•„๋ฌด ๊ฒƒ๋„ ์Œ“์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์€ ์ข…๋ฃŒ๋œ๋‹ค.

์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์œ„์˜ ์ฝ˜์†”์— ์ถœ๋ ฅ๋œ ์ธ์ ‘ ํ–‰๋ ฌ ํ…Œ์ด๋ธ”์„ ํ™•์ธํ•ด๋ณด๋ฉด ์ฒซ ๋ฒˆ์งธ ๋‹จ์ง€๋งŒ ํƒ์ƒ‰ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ํƒ์ƒ‰ํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ฒซ ๋ฒˆ์งธ ๋‹จ์ง€์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ ์œ„์น˜๋ฅผ ๊ธฐ์ค€(arr[2][2], x = 2, y = 2)์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ ํ–ˆ์„ ๋•Œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ Queue์— ์Œ“์ด์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚˜๋ฉด์„œ ํ•ด๋‹น ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ๋œ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋„ˆ๋น„๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊นŠ์ด๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ํƒ์ƒ‰ํ•ด์•ผ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์ง€๋งŒ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์— ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•ด์•ผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์ฒ˜์Œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•œ ์ขŒํ‘œ์˜ ๋…ผ๋ฆฌ๊ฐ€ true ์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹น ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ List ์ž๋ฃŒํ˜•์— ๋‹ด๋Š”๋‹ค.

 

๊ทธ๋Ÿฌ๋ฉด ๋ฌธ์ œ ํ•ด๊ฒฐ!

 

์ด์ œ DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•ด๊ฒฐํ•œ ์ฝ”๋“œ๋ฅผ ํ™•์ธ ํ•ด๋ณด์ž.

๊ฐ€๋ผ! DFS๋ชฌ!

[system]์„ฑ๊ณต์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค!

package ์‹ฌ์„ฑํ—Œ.์•Œ๊ณ ๋ฆฌ์ฆ˜_5์ฃผ์ฐจ;

import java.io.*;
import java.util.*;

public class ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ_2667 {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int n, x, y;
    static int length = 0;
    static int cnt = 0;
    static int[][] arr;

    public static void main(String[] args) throws Exception {
        n = Integer.parseInt(br.readLine());
        arr = new int[n][n];
        StringTokenizer[] st = new StringTokenizer[n];
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            st[i] = new StringTokenizer(br.readLine());
            char[] tmp = st[i].nextToken().toCharArray();
            for (int k = 0; k < tmp.length; k++) {
                arr[i][k] = tmp[k] - '0';
            }
        }
        for (int i = 0; i < arr.length; i++) {
            boolean check = false;
            for (int k = 0; k < arr[i].length; k++) {
                check = dfs(i, k);
                System.out.println("---------------------------");
                if (check) {
                    list.add(cnt);
                    length++;
                    cnt = 0;
                }
            }
        }
        System.out.println(length);
        Collections.sort(list);
        for (int i : list) {
            System.out.println(i);
        }
    }

    public static boolean dfs(int x, int y) {
        if (x < 0 || y < 0 || x >= n || y >= n)
            return false;

        System.out.println("arr[" + x + "][" + y + "] = " + arr[x][y]);
        if (arr[x][y] == 1) {
            arr[x][y] = 5;
            dfs(x - 1, y);
            dfs(x + 1, y);
            dfs(x, y - 1);
            dfs(x, y + 1);
            cnt++;
            return true;
        }
        return false;
    }
}

๋จผ์ € ๊ฒฐ๊ณผ ์ถœ๋ ฅ์„ ํ™•์ธ ํ•ด๋ณด์ž.

์œ„ ์Šคํฌ๋ฆฐ์ƒท์€ ์ ์„ ์„ ๊ธฐ์ค€์œผ๋กœ ํ˜„์žฌ์˜ ๋…ธ๋“œ๊ฐ€ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•  ๋•Œ ์žฌ๊ท€๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ถœ๋ ฅํ•œ ๊ฒฐ๊ณผ๋‹ค. ์ฆ‰, Stack ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ ํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

(์‚ฌ์‹ค DFS๋ฅผ Stack ์ž๋ฃŒ ๊ตฌ์กฐ์™€ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ด์•ผ๊ธฐ๋ฅผ ๋“ฃ๊ณ  ๋‘˜์˜ ์ฐจ์ด๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ „ํ˜€ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ์— ๋น„๋กœ์†Œ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.)

 

main ๋ฉ”์†Œ๋“œ์—์„œ ์ธ์ ‘ ํ–‰๋ ฌ์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๊ตฌ์„ฑํ•˜๊ณ  ๊ทธ ๋งŒํผ dfs ๋ฉ”์†Œ๋“œ๋ฅผ ์ขŒํ‘œ์— ๋งž๊ฒŒ ํ˜ธ์ถœํ•œ๋‹ค. ์ฆ‰, ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
๊ทธ๋ ‡๋‹ค๋ฉด ์œ„์—์„œ Stack ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„ ํ–ˆ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ด ๋ง์˜ ์˜๋ฏธ๋Š” ๋ญ˜๊นŒ?

Stack ์ž๋ฃŒ ๊ตฌ์กฐ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ FILO(First In Last Out, ์„ ์ž…ํ›„์ถœ) ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.
๋งŒ์•ฝ ์œ„์˜ ๋ฌธ์ œ๋ฅผ Stack ์ž๋ฃŒ ๊ตฌ์กฐ๋กœ ํ•ด๊ฒฐํ–ˆ์„ ๋•Œ, ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•œ๋‹ค๋ฉด Stack ์ž๋ฃŒํ˜•์— ๋…ธ๋“œ์˜ ์ขŒํ‘œ๋ฅผ push ํ–ˆ์„ ๊ฒƒ์ด๋‹ค.
๊ทธ ํ›„, Stack์— ์Œ“์ธ ๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ํ•œ ๋ฒˆ ๋ฐ˜๋ณต์„ ์‹คํ–‰ํ•  ๋•Œ pop์„ ํ†ตํ•ด ๊บผ๋‚ธ ๋…ธ๋“œ ์ขŒํ‘œ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ๋‹ค์‹œ ํƒ์ƒ‰ํ•˜๊ณ , ์ด ๋•Œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•œ ๋…ธ๋“œ ์ขŒํ‘œ๋ฅผ ๋‹ค์‹œ Stack์— push ํ•œ๋‹ค.

 

์ฆ‰, ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์€ ๊ทธ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๋จผ์ € ํƒ์ƒ‰ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ํ•˜๋…ธ์ด์˜ ํƒ‘์„ ์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ํ’€์—ˆ๋˜ ๊ธฐ์–ต์„ ๋˜์‚ด๋ ค ๋ณด์ž.

์žฌ๊ท€ ํ•จ์ˆ˜ ๋ณต์Šตํ•˜๊ธฐ

 

3-3) ์žฌ๊ท€ ํ•จ์ˆ˜, ํ•˜๋…ธ์ด์˜ ํƒ‘

์žฌ๊ท€ ํ•จ์ˆ˜

www.notion.so

๋ถ„๋ช… Stack ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ฝ”๋“œ ๊ตฌ์กฐ์™€ ๋ฐฉํ–ฅ์„ฑ์ด ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ–ˆ์„ ๋•Œ์™€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด JAVA๋ฅผ ์ด์šฉํ–ˆ์„ ๋•Œ ๊ตณ์ด Stack ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๋ฐ˜๋ณต๋ฌธ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์„œ ์ฝ”๋“œ๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ๋ณด๋‹ค ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ํ›จ์”ฌ ์ฟจํ•œ ์ฝ”๋“œ๊ฐ€ ๋˜์ง€ ์•Š์„๊นŒ?

 

๊ทธ๋ž˜์„œ ์žฌ๊ท€์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

์ด ๋•Œ, ๋‹จ์ง€์˜ ๊ฐœ์ˆ˜๋ฅผ ๋จผ์ € ์ถœ๋ ฅํ•˜๊ณ , ๊ฐ ๋‹จ์ง€์— ์กด์žฌํ•˜๋Š” ์•„ํŒŒํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋Š”๋ฐ ์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ์ฝ”๋“œ๋ฅผ ์‹ธ๋Š” ๊ฒƒ๋ณด๋‹ค ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ๊ทœ์น™์„ฑ์„ ์ฐพ๋Š” ๊ฒƒ์ด ๋” ์–ด๋ ค์› ๋‹ค.

 

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์ด๋ ‡๋‹ค.

  1. ๋‹จ์ง€ ๋‚ด ์•„ํŒŒํŠธ ๊ฐœ์ˆ˜ ํŒŒ์•…ํ•˜๊ธฐ
    • ๋‹จ์ง€ ๋‚ด์— ์กด์žฌํ•˜๋Š” ์•„ํŒŒํŠธ์˜ ๊ฐœ์ˆ˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ ๋‚ด์—์„œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•  ๋•Œ๋งˆ๋‹ค cnt++ ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    • ๋‹จ์ง€๊ฐ€ ๋๋‚œ ํ›„์— cnt ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ•˜์ง€ ์•Š์œผ๋ฉด ์ „์ฒด ์•„ํŒŒํŠธ ๊ฐœ์ˆ˜๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ผด์ด ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜ ์กฐ๊ฑด์˜ ์—ฐ์žฅ์„ ์œผ๋กœ ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ํ˜ธ์ถœํ•œ ์žฌ๊ท€ ํ•จ์ˆ˜์˜ ๋…ผ๋ฆฌ๊ฐ’์ด true ์ผ ๊ฒฝ์šฐ cnt ๊ฐ’์„ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.
  2. ๋ช‡ ๊ฐœ์˜ ๋‹จ์ง€๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ
    • ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์•„ํŒŒํŠธ๋ผ๋ฉด ํ•ด๋‹น ์•„ํŒŒํŠธ์˜ ๋‹จ์ง€๋งŒํผ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๊ฒƒ์ด๋ฉฐ, ์ด๋Š” ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ํ˜ธ์ถœํ•œ ์žฌ๊ท€ ํ•จ์ˆ˜์˜ ๋…ผ๋ฆฌ๊ฐ’์€ true๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.
    • ๊ทธ๋ ‡๋‹ค๋ฉด true๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ๋‹จ์ง€์˜ ๊ฐœ์ˆ˜๋ฅผ +1 ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์œ„์˜ ์ถœ๋ ฅ๋ฌธ์„ ํ†ตํ•ด ํŒŒ์•…ํ•œ ๋‹จ์ง€ ๋‚ด ์•„ํŒŒํŠธ์˜ ๊ฐœ์ˆ˜, ๋‹จ์ง€์˜ ๊ฐœ์ˆ˜๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.


๋ฌธ์ œ์—์„œ๋Š” ๋‹จ์ง€ ๋‚ด ์•„ํŒŒํŠธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ, ์ด๊ฑธ ๋˜ ์ต์ˆ™ํ•œ ๋ฒ„๋ธ” ์ •๋ ฌ์ด๋‚˜ ์„ ํƒ ์ •๋ ฌ๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ๊ท€์ฐฎ์•„์„œ Collentions ๊ฐ์ฒด์˜ sort() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์ด๋ฅผ ์ถœ๋ ฅํ–ˆ๋‹ค.

 

์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์กฐ๊ธˆ ์ดํ•ด๋˜๋˜ ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ์กฐ๊ธˆ ๋” ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
BFS, DFS ๋‘ ๊ฒฝ์šฐ ๋ชจ๋‘ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ–ˆ๊ณ , ์ด๋ ‡๊ฒŒ ๊ธ€์„ ์ผ๊ธฐ ๋•Œ๋ฌธ์— ์•ž์œผ๋กœ ๋‚˜์•„๊ฐˆ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค!
์•„์ง ๊ทธ๋ž˜ํ”„๋ฅผ ์™„์ „ํžˆ ์ดํ•ดํ•˜๊ธฐ์—๋Š” ๊นŒ๋งˆ๋“ํ•˜์ง€๋งŒ ๋Š๋ ค๋„ ๋งž๋Š” ๋ฐฉํ–ฅ์„ ํ–ฅํ•ด ๊ฑธ์–ด๊ฐ€์ž!