ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준] 15683. 감시
    PS/Java 2022. 3. 29.

    스타트링크의 사무실은 1×1크기의 정사각형으로 나누어져 있는 N×M 크기의 직사각형으로 나타낼 수 있다. 사무실에는 총 K개의 CCTV가 설치되어져 있는데, CCTV는 5가지 종류가 있다. 각 CCTV가 감시할 수 있는 방법은 다음과 같다.

    1번 2번 3번 4번 5번

    1번 CCTV는 한 쪽 방향만 감시할 수 있다. 2번과 3번은 두 방향을 감시할 수 있는데, 2번은 감시하는 방향이 서로 반대방향이어야 하고, 3번은 직각 방향이어야 한다. 4번은 세 방향, 5번은 네 방향을 감시할 수 있다.

     

    CCTV는 감시할 수 있는 방향에 있는 칸 전체를 감시할 수 있다. 사무실에는 벽이 있는데, CCTV는 벽을 통과할 수 없다. CCTV가 감시할 수 없는 영역은 사각지대라고 한다.

     

    CCTV는 회전시킬 수 있는데, 회전은 항상 90도 방향으로 해야 하며, 감시하려고 하는 방향이 가로 또는 세로 방향이어야 한다.

    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 1 0 6 0
    0 0 0 0 0 0

    지도에서 0은 빈 칸, 6은 벽, 1~5는 CCTV의 번호이다. 위의 예시에서 1번의 방향에 따라 감시할 수 있는 영역을 '#'로 나타내면 아래와 같다.

    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 1 # 6 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    # # 1 0 6 0
    0 0 0 0 0 0
    0 0 # 0 0 0
    0 0 # 0 0 0
    0 0 1 0 6 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 1 0 6 0
    0 0 # 0 0 0

     

    CCTV는 벽을 통과할 수 없기 때문에, 1번이 → 방향을 감시하고 있을 때는 6의 오른쪽에 있는 칸을 감시할 수 없다.

    0 0 0 0 0 0
    0 2 0 0 0 0
    0 0 0 0 6 0
    0 6 0 0 2 0
    0 0 0 0 0 0
    0 0 0 0 0 5

    위의 예시에서 감시할 수 있는 방향을 알아보면 아래와 같다.

     

     

     

     

     

     

     

     

     

    CCTV는 CCTV를 통과할 수 있다. 아래 예시를 보자.

    0 0 2 0 3
    0 6 0 0 0
    0 0 6 6 0
    0 0 0 0 0

    위와 같은 경우에 2의 방향이 ↕ 3의 방향이 ←와 ↓인 경우 감시받는 영역은 다음과 같다.

    # # 2 # 3
    0 6 # 0 #
    0 0 6 6 #
    0 0 0 0 #

    사무실의 크기와 상태, 그리고 CCTV의 정보가 주어졌을 때, CCTV의 방향을 적절히 정해서, 사각 지대의 최소 크기를 구하는 프로그램을 작성하시오.


    예제 입력 4

    6 6
    1 0 0 0 0 0
    0 1 0 0 0 0
    0 0 1 5 0 0
    0 0 5 1 0 0
    0 0 0 0 1 0
    0 0 0 0 0 1

    예제 출력 4

    2

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.StringTokenizer;
    public class Main {
        static int N, M;
        static int[][] arr;
        static List<CCTV> list;
        static int[] number;
        static int min = Integer.MAX_VALUE;
        static class CCTV {
            int r;
            int c;
            int status;
    
            public CCTV(int r, int c, int status) {
                this.r = r;
                this.c = c;
                this.status = status;
            }
        }
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(br.readLine());
    
            N = Integer.parseInt(st.nextToken());
            M = Integer.parseInt(st.nextToken());
    
            arr = new int[N][M];
            list = new LinkedList<>();
    
            for(int i = 0; i < N; i++) {
                st = new StringTokenizer(br.readLine());
                for(int j = 0; j < M; j++) {
                    arr[i][j] = Integer.parseInt(st.nextToken());
                    if(arr[i][j] >= 1 && arr[i][j] <= 5) {
                        list.add(new CCTV(i, j, arr[i][j]));
                    }
                 }
            }
    
            number = new int[list.size()];
            dfs(0);
            System.out.println(min);
        }
    
        public static void dfs(int cnt) {
            if(cnt == list.size()) {
                bfs(number);
                return;
            }
    
            for(int i = 0; i < 4; i++) {
                number[cnt] = i;
                dfs(cnt + 1);
            }
        }
        static int[] cr = {-1, 1, 0, 0};
        static int[] cc = {0, 0, -1, 1};
        static int[][][] cdir = {
                                {{0}, {1}, {2}, {3}},
                                {{0, 1}, {2, 3}},
                                {{0, 3}, {3, 1}, {1, 2}, {2, 0}},
                                {{2, 0, 3}, {0, 3, 1}, {3, 1, 2}, {1, 2, 0}},
                                {{0, 1, 2, 3}}
                                };
        public static void bfs(int[] number) {
            for(int i = 0; i < list.size();i++) {
                CCTV current = list.get(i);
                int r = number[i];
                if(current.status == 2) {
                    r = number[i] % 2;
                } else if (current.status == 5) {
                    r = 0;
                }
                for(int d = 0; d < cdir[current.status - 1][r].length; d++) {
                    int nr = current.r;
                    int nc = current.c;
                    int dir = cdir[current.status - 1][r][d];
    
                    while (true) {
                        nr += cr[dir];
                        nc += cc[dir];
                        if(nr < 0 || nc < 0 || nr >= N || nc >= M || arr[nr][nc] == 6) {
                            break;
                        }
                        if(arr[nr][nc] == 0) {
                            arr[nr][nc] = 7;
                        }
                    }
                }
            }
            int count = 0;
            for(int i = 0; i < N; i++) {
                for(int j = 0; j < M; j++) {
                    if(arr[i][j] == 0) {
                        count++;
                    } else if (arr[i][j] == 7) {
                        arr[i][j] = 0;
                    }
                }
            }
            min = Math.min(min, count);
        }
    }

    CCTV의 위치와 상태를 가지는 클래스

    static class CCTV {
        int r;
        int c;
        int status;
    
        public CCTV(int r, int c, int status) {
            this.r = r;
            this.c = c;
            this.status = status;
        }
    }

    방향을 나타내는 cr,  cc배열과 cctv번호에 맞는 방향의 cr,cc에 접근하는 index를 가지는 cdir배열을 이용한다.

    static int[] cr = {-1, 1, 0, 0};
    static int[] cc = {0, 0, -1, 1};
    static int[][][] cdir = {
                            {{0}, {1}, {2}, {3}},
                            {{0, 1}, {2, 3}},
                            {{0, 3}, {3, 1}, {1, 2}, {2, 0}},
                            {{2, 0, 3}, {0, 3, 1}, {3, 1, 2}, {1, 2, 0}},
                            {{0, 1, 2, 3}}
                            };

     

    'PS > Java' 카테고리의 다른 글

    [백준] 2589. 보물섬  (0) 2022.03.30
    [백준] 12865. 평범한 배낭  (0) 2022.03.29
    [백준] 16985. Maaaaaaaaaze  (0) 2022.03.29
    [백준] 1149. RGB거리  (0) 2022.03.29
    [백준] 11399. ATM  (0) 2022.03.29

    댓글

Designed by Tistory.