반응형

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

 

15683번: 감시

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

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. cctv가 있는 위치를 vector에 저장하고 총 개수를 limit변수에 저장합니다.

 

2. 재귀 호출을 통해 모든 cctv의 방향을 설정하고, #으로 바꿉니다.

 

3. 모든 cctv의 방향을 설정했다면 사각지대의 개수를 세어 ans와 비교해 저장합니다.

 

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<iostream>
#include<vector>
 
using namespace std;
 
int N, M;
int arr[8][8];
int limit;
vector<pair<intint>> pos;
int ans = 987654321;
 
void cctv(int y, int x, int dir)    //cctv가 보는 방향을 #으로 초기화
{
    if (dir == 0) {
        for (int i = 1; i < M; i++) {
            if (x + i >= M) break;
            int temp = arr[y][x + i];
            if (temp == 6) {
                break;
            }
            else if (temp > 0 && temp < 6) {
                continue;
            }
            else {
                arr[y][x + i] = '#';
            }
 
        }
    }
    else if (dir == 1) {
        for (int i = 1; i < N; i++) {
            if (y + i >= N) break;
            int temp = arr[y + i][x];
            if (temp == 6) {
                break;
            }
            else if (temp > 0 && temp < 6) {
                continue;
            }
            else {
                arr[y + i][x] = '#';
            }
 
        }
    }
    else if (dir == 2) {
        for (int i = 1; i < M; i++) {
            if (x - i < 0break;
            int temp = arr[y][x - i];
            if (temp == 6) {
                break;
            }
            else if (temp > 0 && temp < 6) {
                continue;
            }
            else {
                arr[y][x - i] = '#';
            }
 
        }
    }
    else {
        for (int i = 1; i < N; i++) {
            if (y - i < 0break;
            int temp = arr[y - i][x];
            if (temp == 6) {
                break;
            }
            else if (temp > 0 && temp < 6) {
                continue;
            }
            else {
                arr[y - i][x] = '#';
            }
 
        }
    }
}
 
int blind()    //사각지대 개수 세기
{
    int ret = 0;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (arr[i][j] == 0) {
                ret++;
            }
        }
    }
 
    return ret;
}
 
void dfs(int level)
{
    if (level == limit) {
        int cnt = blind();
        ans = min(ans, cnt);
        return;
    }
 
    int temp[8][8= { 0 };
    for (int i = 0; i < N; i++) {    //백트래킹을 위한 저장
        for (int j = 0; j < M; j++) {
            temp[i][j] = arr[i][j];
        }
    }
 
    int y = pos[level].first;
    int x = pos[level].second;
    int num = arr[y][x];
 
    if (num == 1) {    //1번 cctv
        for (int i = 0; i < 4; i++) {
            cctv(y, x, i);
            dfs(level + 1);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    arr[i][j] = temp[i][j];
                }
            }
        }
    }
    else if (num == 2) {    //2번 cctv
        for (int i = 0; i < 2; i++) {
            if (i == 0) {
                cctv(y, x, 0);
                cctv(y, x, 2);
            }
            else {
                cctv(y, x, 1);
                cctv(y, x, 3);
            }
            dfs(level + 1);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    arr[i][j] = temp[i][j];
                }
            }
        }
    }
    else if (num == 3) {    //3번 cctv
        for (int i = 0; i < 4; i++) {
            int dir1 = 0 + i;
            int dir2 = 1 + i;
            if (dir2 > 3) {
                dir2 -= 4;
            }
            cctv(y, x, dir1);
            cctv(y, x, dir2);
            dfs(level + 1);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    arr[i][j] = temp[i][j];
                }
            }
        }
    }
    else if (num == 4) {    //4번 cctv
        for (int i = 0; i < 4; i++) {
            int dir1 = 0 + i;
            int dir2 = 1 + i;
            int dir3 = 2 + i;
            if (dir2 > 3) {
                dir2 -= 4;
            }
            if (dir3 > 3) {
                dir3 -= 4;
            }
            cctv(y, x, dir1);
            cctv(y, x, dir2);
            cctv(y, x, dir3);
            dfs(level + 1);
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    arr[i][j] = temp[i][j];
                }
            }
        }
    }
    else {    //5번 cctv
        for (int i = 0; i < 4; i++) {
            cctv(y, x, i);
        }
        dfs(level + 1);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                arr[i][j] = temp[i][j];
            }
        }
    }
}
 
int main()
{
    scanf("%d %d"&N, &M);
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            scanf("%d"&arr[i][j]);
            if (arr[i][j] > 0 && arr[i][j] < 6) {
                pos.push_back({ i, j });
                limit++;
            }
        }
    }
 
    dfs(0);
 
    printf("%d", ans);
}
cs
반응형

+ Recent posts