티스토리 뷰

출저 : https://www.acmicpc.net/problem/15683


"구현, 브루트 포스"



아래와 같이 구현했다. 삼성 기출 문제. 특정 위치에 있는 CCTV가 종류별로 있는데, 이를 4방향으로 돌려가며

사각지대 0의 개수를 최소로 줄이는 경우를 찾는 것이다. 아래 풀이에서는 각 CCTV 마다 한 방향만을 설정해주었는데, 회전 가능한 4방향에 대해서 다 정의해 준다면,  2번 CCTV 는 2회, 5번 CCTV는 1회로 횟수를 더욱 줄일 수 있다.

근데 나는 귀찮아서 그냥 4방향 다 돌렸따....

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Main {
 
    static int N,M;
    static int cnt;
    static int min = Integer.MAX_VALUE;
    static Node[] nodes = new Node[8];
    static int[][] dir = {
        {},
        {1},
        {1,3},
        {0,1},
        {0,1,3},
        {0,1,2,3}
    };
    static int[] dx = {-1,0,1,0};
    static int[] dy = {0,1,0,-1};
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        
        int[][] map = new int[N][M];
        
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if(map[i][j] >= 1 && map[i][j] <= 5) {
                    nodes[cnt++= new Node(i, j, map[i][j]);
                }
            }
        }
        
        solve(0, map);
        System.out.println(min);
    }
    
    static void solve(int idx, int[][] arr) {
        if(idx == cnt) {
            int res = 0;
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    if(arr[i][j] == 0)
                        res++;
                }
            }
            min = Math.min(min, res);
            return ;
        }
        Node node = nodes[idx];
        
        // 왼쪽으로 4번 돌린다.
        for (int d = 0; d < 4; d++) {
            int[][] map = copy(arr);
            
            // 해당 CCTV가 감시하는 모든 방향에 대해 체크 한다.
            // 1,2,3,4  방향이 될 수 있다.
            for (int i = 0; i < dir[node.c].length; i++) {
                int nd = (dir[node.c][i]-+ 3) % 4;
                int nx = node.x;
                int ny = node.y;
                
                while(true) {
                    nx += dx[nd];
                    ny += dy[nd];
                    if(!isRange(nx, ny) || map[nx][ny] == 6break;
                    map[nx][ny] = 7;
                }
            }
            solve(idx+1, map);
        }
    }
    
    static boolean isRange(int x, int y) {
        if(x < 0 || x >= N || y < 0 || y >= M) return false;
        return true;
    }
    
    static int[][] copy(int[][] arr) {
        int[][] map = new int[N][M];
        
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                map[i][j] = arr[i][j];
            }
        }
        return map;
    }
}
 
class Node {
    int x;
    int y;    
    int c;    // cctv
    
    Node (int x, int y, int c) {
        this.x = x;
        this.y = y;
        this.c = c;
    }
}
 
cs





댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday