์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[๋ฐฑ์ค€/1012/์œ ๊ธฐ๋† ๋ฐฐ์ถ”]

์‹œํ๋ฆฌํ‹ฐ์ง€ํ˜ธ 2024. 3. 25. 20:31

https://www.acmicpc.net/problem/1012

 

1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

์ฐจ์„ธ๋Œ€ ์˜๋†์ธ ํ•œ๋‚˜๋Š” ๊ฐ•์›๋„ ๊ณ ๋žญ์ง€์—์„œ ์œ ๊ธฐ๋† ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋†์•ฝ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ฐฐ์ถ”๋ฅผ ์žฌ๋ฐฐํ•˜๋ ค๋ฉด ๋ฐฐ์ถ”๋ฅผ ํ•ด์ถฉ์œผ๋กœ๋ถ€ํ„ฐ ๋ณดํ˜ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ๋‚˜๋Š” ํ•ด์ถฉ ๋ฐฉ์ง€์— 

www.acmicpc.net

<<ํ’€์ด>>

 

์šฐ์„  ์ด ๋ฌธ์ œ๋Š” DFS๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ฐจ์› ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด์„œ 

 

DFSํ˜•ํƒœ๋กœ ๊ตฌ์ƒ์„ ํ–ˆ๋‹ค.

 

๋งŒ์•ฝ ํ•ด๋‹น ๋ฐฐ์—ด์— ๊ฐ’์ด 1์ด๋ผ๋ฉด dx, dy๋กœ ์ƒํ•˜ ์ขŒ์šฐ์— 1์ธ์ง€ ์•„๋‹Œ์ง€ ์ตœ์ข…์ ์œผ๋กœ ์—†์„ ๋•Œ ๊นŒ์ง€ ๊นŠ๊ฒŒ ๋“ค์–ด๊ฐ„๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ ๋“ค์–ด๊ฐ„ ๊ฐ’์€ ch์— ๊ฐ™์€ ์ขŒํ‘œ์— 1์„ ์ถ”๊ฐ€ํ•จ์œผ๋กœ์จ ์ค‘๋ณต๊ณ„์‚ฐ ๋˜๋Š” ๊ฒƒ์„ ๋ง‰์œผ๋ ค ํ–ˆ๋‹ค.

 

๋…ผ๋ฆฌ์ ์œผ๋กœ๋Š” ํ‹€๋ฆฐ๊ฒŒ ์—†์–ด๋ณด์ด์ง€๋งŒ..

 

ํ‹€๋ ธ๋‹ค ใ… 

 

์™ค๊นŒ?

import java.util.Scanner;

class Main {

        public static int[][] ch;
        public static int count;
        public static int[] dx = {-1, 1, 0, 0};
        public static int[] dy = {0, 0, -1, 1};


        private void DFS(int n, int m, int[][] arr, int N, int M) {
                if (N >= 0 && N < n && M >= 0 && M < m && arr[N][M] == 1 && ch[N][M] != 1) {
                        ch[N][M] = 1;
                        DFS(n, m, arr, n+dx[0], m+dy[0]);
                        DFS(n, m, arr, n+dx[1], m+dy[1]);
                        DFS(n, m, arr, n+dx[2], m+dy[2]);
                        DFS(n, m, arr, n+dx[3], m+dy[3]);
                } else return;
        }

        public static void main(String[] args) {
                Main T = new Main();
                Scanner in = new Scanner(System.in);
                int test = in.nextInt();


                for (int i = 0; i < test; i++) {
                        int m = in.nextInt();
                        int n = in.nextInt();
                        int where = in.nextInt();
                        int[][] arr = new int[n][m];
                         ch = new int[n][m];

                        for (int j = 0; j < where; j++) {
                                int b = in.nextInt();
                                int a = in.nextInt();

                               arr[a][b] = 1;
                        }

                        for (int j = 0; j < n; j++) {
                                for (int k = 0; k < m; k++) {
                                        if (arr[j][k] == 1 && ch[j][k] != 1) {
                                                ch[j][k] = 1;
                                                T.DFS(n, m, arr, j+dx[0], k+dy[0]);
                                                T.DFS(n, m, arr, j+dx[1], k+dy[1]);
                                                T.DFS(n, m, arr, j+dx[2], k+dy[2]);
                                                T.DFS(n, m, arr, j+dx[3], k+dy[3]);
                                                count++;
                                        }
                                }
                        }
                        System.out.println(count);

                        ch = new int[n][m];
                        count = 0;
                }
        }
}

 

ใ…‹ใ…‹ ์•Œ๊ณ ๋ณด๋‹ˆ๊นŒ ๊ฐ’์„ ์ž˜๋ชป ๋Œ€์ž…ํ–ˆ๋‹ค ใ…Žใ…Ž..

 

 

์œ„ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž˜ ๋ณด๋ฉด N์ด ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ๋ฅผ n์œผ๋กœ ํ•˜๊ณ  M์€ m์„ ๋„ฃ์€ ์…ˆ์ด๋‹ค.

 

์ฝ”๋”ฉ์ด ๊ธธ์–ด์ง€๊ณ  ๋ฃจ์ฆˆํ•ด์ง€๋‹ค ๋ณด๋‹ˆ๊นŒ ํ—ท๊ฐˆ๋ ธ๋‚˜๋ณด๋‹ค !!

 

์ด๋ž˜์„œ ์ฝ”๋”ฉ์„ ํ•  ๋•Œ ์ด๋ฆ„ ์„ ์ •๋„ ์ค‘์š”ํ•œ ๋ฌธ์ œ์ธ ๊ฒƒ์ด๋‹ค.

 

์ž‘์„ฑํ•  ๋•Œ ๊ท€์ฐฎ๋”๋ผ๋„ ์ œ๋Œ€๋กœ ์ž‘์„ฑํ•˜๊ณ  ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ฒŒ ํ•ด์•ผ๊ฒ ๋‹ค.

 

๋ฐ”๊พธ๋‹ˆ ๋ฐ”๋กœ ์ •๋‹ต์ด๋‹ค ~!

import java.util.ArrayList;
import java.util.Scanner;

class Main {

        public static int[][] ch;
        public static int count;
        public static int[] dx = {-1, 1, 0, 0};
        public static int[] dy = {0, 0, -1, 1};


        private void DFS(int n, int m, int[][] arr, int N, int M) {
                if (N >= 0 && N < n && M >= 0 && M < m && arr[N][M] == 1 && ch[N][M] != 1) {
                        ch[N][M] = 1;
                        DFS(n, m, arr, N+dx[0], M+dy[0]);
                        DFS(n, m, arr, N+dx[1], M+dy[1]);
                        DFS(n, m, arr, N+dx[2], M+dy[2]);
                        DFS(n, m, arr, N+dx[3], M+dy[3]);
                } else return;
        }

        public static void main(String[] args) {
                Main T = new Main();
                Scanner in = new Scanner(System.in);
                int test = in.nextInt();
                ArrayList<Integer> answer = new ArrayList<>();


                for (int i = 0; i < test; i++) {
                        int m = in.nextInt();
                        int n = in.nextInt();
                        int where = in.nextInt();
                        int[][] arr = new int[n][m];
                         ch = new int[n][m];

                        for (int j = 0; j < where; j++) {
                                int b = in.nextInt();
                                int a = in.nextInt();

                               arr[a][b] = 1;
                        }

                        for (int j = 0; j < n; j++) {
                                for (int k = 0; k < m; k++) {
                                        if (arr[j][k] == 1 && ch[j][k] != 1) {
                                                ch[j][k] = 1;
                                                T.DFS(n, m, arr, j+dx[0], k+dy[0]);
                                                T.DFS(n, m, arr, j+dx[1], k+dy[1]);
                                                T.DFS(n, m, arr, j+dx[2], k+dy[2]);
                                                T.DFS(n, m, arr, j+dx[3], k+dy[3]);
                                                count++;
                                        }
                                }
                        }

                        answer.add(count);

                        ch = new int[n][m];
                        count = 0;
                }

                for(int x : answer) System.out.println(x);
        }
}

 

์•„๋ž˜ ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋„ ์ •๋‹ต์ด๋‹ค.

์ƒ๊ฐํ•ด๋ณด๋ฉด ์ €๋ ‡๊ฒŒ ์ฒ˜์Œ์— ์‹œ์ž‘ํ•ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

์‹œ์ž‘์ ์ด ๋‹ค๋ฅธ ๊ฒƒ ๋ฟ์ด๋‹ค.

 

์•„์ง ์ต์ˆ™ํ•˜์ง€ ์•Š์œผ๋‹ˆ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง€๊ณ  ์“ธ๋ฐ ์—†์ด ๊ธธ์–ด์ง€๋Š” ์ƒํ™ฉ์ธ ๊ฒƒ์ด๋‹ค.

 

๊พธ์ค€ํžˆ ์—ฐ์Šตํ•ด์„œ ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„ ๋˜ํ•œ ๋ณด์™„ํ•ด ๋‚˜๊ฐ€์ž !!

import java.util.ArrayList;
import java.util.Scanner;

class Main {

        public static int[][] ch;
        public static int count;
        public static int[] dx = {-1, 1, 0, 0};
        public static int[] dy = {0, 0, -1, 1};


        private void DFS(int n, int m, int[][] arr, int N, int M) {
                if (N >= 0 && N < n && M >= 0 && M < m && arr[N][M] == 1 && ch[N][M] != 1) {
                        ch[N][M] = 1;
                        DFS(n, m, arr, N+dx[0], M+dy[0]);
                        DFS(n, m, arr, N+dx[1], M+dy[1]);
                        DFS(n, m, arr, N+dx[2], M+dy[2]);
                        DFS(n, m, arr, N+dx[3], M+dy[3]);
                } else return;
        }

        public static void main(String[] args) {
                Main T = new Main();
                Scanner in = new Scanner(System.in);
                int test = in.nextInt();
                ArrayList<Integer> answer = new ArrayList<>();


                for (int i = 0; i < test; i++) {
                        int m = in.nextInt();
                        int n = in.nextInt();
                        int where = in.nextInt();
                        int[][] arr = new int[n][m];
                         ch = new int[n][m];

                        for (int j = 0; j < where; j++) {
                                int b = in.nextInt();
                                int a = in.nextInt();

                               arr[a][b] = 1;
                        }

                        for (int j = 0; j < n; j++) {
                                for (int k = 0; k < m; k++) {
                                        if (arr[j][k] == 1 && ch[j][k] != 1) {
                                                T.DFS(n, m, arr, j, k);
                                                count++;
                                        }

                                }
                        }

                        answer.add(count);

                        ch = new int[n][m];
                        count = 0;
                }

                for(int x : answer) System.out.println(x);
        }
}

 

์ด ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด์„œ dx, dy๋ฅผ ํ™œ์šฉํ•ด๋ณด์•˜๊ณ , DFS์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜๋Š” ์ข‹์€ ๊ณ„๊ธฐ๊ฐ€ ๋˜์—ˆ๋‹ค.

 

์•„์ง์€ ์ต์ˆ™ํ•˜์ง€ ์•Š์€ DFS BFS ์ต์ˆ™ํ•ด์งˆ ์ˆ˜ ์žˆ๋„๋ก ๊ณ„์† ์—ฐ์Šตํ•˜์ž.