본문 바로가기

시뮬레이션과 구현

감시

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

 

15683번: 감시

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

www.acmicpc.net

 CCTV의 정보가 주어졌을 때, CCTV의 방향을 적절히 정해라!

브루트포스(재귀)로 CCTV의 방향을 바꿔가며

사각지대를 최소화하는 문제.

 

5번 카메라는 방향을 바꿀수없다! 4방향이니까

5번 카메라는 먼저 map에 그려버리자.

 

맵을 되돌려야하는 문제 

보면 맵 크기가 작다 (8 x 8)

 

1번 -> 4방향

2번 -> 2방향

3번 -> 4방향

4번 -> 4방향

 

    // 브루트포스(재귀)
    for (int i = 0; i < 4; i++)
    {
           indexchooser.push_back(i);
           choose();
           indexchooser.pop_back();
    }

 

<백트래킹>

2번일 때 idx가 2, 3이면 -1 return

 
<소스 코드>
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <vector>
#include <cstring>
 
using namespace std;
 
// Up, Right, Down, Left
int dx[] = { -1010 };
int dy[] = { 010-1 };
 
int n, m, range;
int cctv_cnt = 0;
int ans = 0;
int originmap[10][10];
int map[10][10];
 
struct t {
    int r, c, tv_num;
};
 
vector<t> cctv;
 
int two[4][2= {{0,2}, {13}};
int three[4][2= {{0,1}, {12}, {23}, {03}};
int four[4][3= {{012}, {013}, {023}, {123}};
 
vector<int> indexchooser;
 
void Right(int r, int c)
{
    for (int j = c; j < m; j++)
    {
        // 벽 만나면 break
        if (map[r][j] == 6)
            break;
        if (1 <= map[r][j] && map[r][j] <= 5)
            continue;
        if (map[r][j] == 0)
            map[r][j] = -1;
    }
}
 
void Left(int r, int c)
{
    for (int j = c; j >= 0; j--)
    {
        if (map[r][j] == 6)
            break;
        if (1 <= map[r][j] && map[r][j] <= 5)
            continue;
        if (map[r][j] == 0)
            map[r][j] = -1;
    }
}
 
void Up(int r, int c)
{
    for (int i = r; i >= 0; i--)
    {
        if (map[i][c] == 6)
            break;
        if (1 <= map[i][c] && map[i][c] <= 5)
            continue;
        if (map[i][c] == 0)
            map[i][c] = -1;
    }
}
 
void Down(int r, int c)
{
    for (int i = r; i < n; i++)
    {
        if (map[i][c] == 6)
            break;
        if (1 <= map[i][c] && map[i][c] <= 5)
            continue;
        if (map[i][c] == 0)
            map[i][c] = -1;
    }
}
 
void how_to_go(int num, int row, int col)
{
    if (num == 0)
    {
        Up(row, col);
    }
    else if (num == 1)
    {
        Right(row, col);
    }
    else if (num == 2)
    {
        Down(row, col);
    }
    else
    {
        Left(row, col);
    }
}
 
int solve()
{
    int ssap = 0;
    // 원본 map 복사
    memcpy(map, originmap, sizeof(map));
 
    for (int i = 0; i < indexchooser.size(); i++)
    {
        int row = cctv[i].r;
        int col = cctv[i].c;
        int soo = cctv[i].tv_num;
 
        if (soo == 1)
        {
            how_to_go(indexchooser[i], row, col);
        }
        else if (soo == 2)
        {
            // 백트래킹
            if (indexchooser[i] > 1)
                return -1;
 
            int a = two[indexchooser[i]][0];
            int b = two[indexchooser[i]][1];
            how_to_go(a, row, col);
            how_to_go(b, row, col);
        }
        else if (soo == 3)
        {
            int a = three[indexchooser[i]][0];
            int b = three[indexchooser[i]][1];
            how_to_go(a, row, col);
            how_to_go(b, row, col);
        }
        else
        {
            int a = four[indexchooser[i]][0];
            int b = four[indexchooser[i]][1];
            int c = four[indexchooser[i]][2];
            how_to_go(a, row, col);
            how_to_go(b, row, col);
            how_to_go(c, row, col);
        }
    }
 
    // 사각지대 개수 체크
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (map[i][j] == 0)
                ssap++;
        }
    }
    return ssap;
}
 
 
// m C 3을 하자
void choose()
{
    // cctv 개수와 같아졌다면!
    if (indexchooser.size() == cctv_cnt)
    {
        int tmp = solve();
        if ((tmp != - 1&& (tmp < ans))
            ans = tmp;
        return;
    }
 
    // 브루트포스(재귀)
    for (int i = 0; i < 4; i++)
    {
        indexchooser.push_back(i);
        choose();
        indexchooser.pop_back();
    }
    return;
}
 
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> n >> m;
 
    vector<pair<intint>> special;
    bool is_there_five = false;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> map[i][j];
            // 0의 개수를 여기서 저장
            if (map[i][j] == 0)
                ans++;
            originmap[i][j] = map[i][j];
            // 5는 따로 처리
            if (map[i][j] != 0 && map[i][j] == 5)
            {
                is_there_five = true;
                special.push_back(make_pair(i, j));
            }
            else if (map[i][j] != 0 && map[i][j] < 5)
            {
                cctv_cnt++;
                cctv.push_back({ i,j,map[i][j] });
            }
        }
    }
 
    // 5는 영역을 미리 그려버린다
    if (is_there_five)
    {
        for (int i = 0; i < special.size(); i++)
        {
            int x = special[i].first;
            int y = special[i].second;
            Right(x, y);
            Left(x, y);
            Up(x, y);
            Down(x, y);
            memcpy(originmap, map, sizeof(map));
        }
    }
 
    // cctv의 방향을 재귀로 바꾸자
    choose();
    cout << ans << '\n';
 
    return 0;
}
 
cs

'시뮬레이션과 구현' 카테고리의 다른 글

배열 돌리기 4  (1) 2023.09.18
상어 초등학교 (vector 정렬)  (0) 2023.09.15
캐슬 디펜스  (0) 2023.09.06
LCD Test  (0) 2023.08.16
톱니바퀴  (0) 2023.08.01