기술 블로그

16461번 듀얼 채널 VHF 무전기 본문

알고리즘 문제/BOJ

16461번 듀얼 채널 VHF 무전기

parkit 2018. 11. 27. 09:09
728x90
반응형

제 5회 한양대학교 프로그래밍 경시대회 Open Contest - Advanced Div. B번 문제


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


문제를 제대로 안 읽어서 살짝 고생했다.


주파수 조절은 세 가지 전략 중에서 하나를 선택하여 이루어진다.


2가지 풀이 방법이 있다.

① BFS - 내가 푼 방법

② 시뮬레이션 - 다른 분의 코드를 참고하였다.




① BFS

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
 
using namespace std;
 
double A = 0.0, B = 0.0, goal = 0.0;
 
char now;
 
int down()
{
    queue<pair<intchar> > q;
 
    bool visit[2002= { false, };
 
    memset(visit, falsesizeof(visit));
 
    // 현재 채널에 따라 경우의 수가 2가지로 나눌 수 있다.
    if (now == 'A')
    {
        q.push({ (int)A, now });
        visit[(int)A] = true;
 
    }
    else if (now == 'B')
    {
        q.push({ (int)B, now });
        visit[(int)B] = true;
    }
 
    int ret = 0;
 
    while (!q.empty())
    {
        int qSize = q.size();
 
        while (qSize--)
        {
            int present = q.front().first;
            char aorb = q.front().second;
 
            int var = present;
            // 밑에 연산에 의해 present가 변하므로, var을 활용한다.
 
            q.pop();
 
            if (present == (int)goal)
            {
                // 같으면 return
                return ret;
            }
 
            if (aorb == 'A')
            {
                // A/B
                aorb = 'B';
                present = (int)B;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
 
                // down
                aorb = 'A';
                present = var;
                present -= 20;
                if (present < 0) present = 2000;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
            }
            else if (aorb == 'B')
            {
                // A/B
                aorb = 'A';
                present = (int)A;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
 
                // down
                aorb = 'B';
                present = var;
                present -= 20;
                if (present < 0) present = 2000;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
            }
        }
 
        ++ret;
 
        // 어차피 최댓값이 6이므로, 6이상이 되면, 바로 return.
        if (ret >= 6return ret;
    }
}
 
int up()
{
    queue<pair<intchar> > q;
 
    bool visit[2002= { false, };
 
    memset(visit, falsesizeof(visit));
 
    // 현재 채널에 따라 경우의 수가 2가지로 나눌 수 있다.
    if (now == 'A')
    {
        q.push({ (int)A, now });
        visit[(int)A] = true;
 
    }
    else if (now == 'B')
    {
        q.push({ (int)B, now });
        visit[(int)B] = true;
    }
 
    int ret = 0;
 
    while (!q.empty())
    {
        int qSize = q.size();
 
        while (qSize--)
        {
            int present = q.front().first;
            char aorb = q.front().second;
 
            int var = present;
            // 밑에 연산에 의해 present가 변하므로, var을 활용한다.
 
            q.pop();
 
            if (present == (int)goal)
            {
                // 같으면 return
                return ret;
            }
 
            if (aorb == 'A')
            {
                // A/B
                aorb = 'B';
                present = (int)B;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
 
                // up
                aorb = 'A'// 단순 up을 누르는 것이므로, 위와 달리 aorb = 'A'로 다시 설정
                present = var; // 초기 present를 저장해놨던 var 활용.(연산에 의해 변할 수 있기 때문)
                present += 20// up은 +20
                if (present > 2000) present = 0;
                // 146.000(= 2000)을 초과하면, 그 반대편인 144.000(=0)으로 초기화.
                if (!visit[present])
                {
                    // 방문하지 않았다면
 
                    q.push({ present, aorb });
                    visit[present] = true;
                }
            }
            else if (aorb == 'B')
            {
                // A/B
                aorb = 'A';
                present = (int)A;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
 
                // up
                aorb = 'B';
                present = var;
                present += 20;
                if (present > 2000) present = 0;
                if (!visit[present])
                {
                    q.push({ present, aorb });
                    visit[present] = true;
                }
 
            }
        }
 
        ++ret;
 
        // 어차피 최댓값이 6이므로, 6이상이 되면, 바로 return.
        if (ret >= 6return ret;
    }
}
 
int main(void)
{
    int T = 0;
 
    scanf("%d"&T);
 
    for (int i = 0; i < T; i++)
    {
        scanf("%lf %lf %c %lf"&A, &B, &now, &goal);
 
        /*
        144.000 이상 ~ 146.000 이하이므로
        곱하기 1000을 하면
        144000 이상 ~ 146000이하
        빼기 144000
        0 이상 ~ 2000 이하 이므로,
        bool visit[2002]을 쉽게 이용할 수 있다.
        */
 
        A *= 1000;
        B *= 1000;
        goal *= 1000;
 
        A -= 144000;
        B -= 144000;
        goal -= 144000;
 
        int upcnt = up();
        int downcnt = down();
 
        int result = min(upcnt, downcnt);
 
        if (result >= 6) result = 6;
 
        // 6번까지가 최대이다. 어차피 6 이상은 번호를 직접 6개를 누를 수 있기 때문이다.
        if (result >= 6printf("6\n");
        else printf("%d\n", result);
    }
 
    return 0;
}
cs







② 시뮬레이션

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
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
 
using namespace std;
 
double A = 0.0, B = 0.0, goal = 0.0;
 
char now;
 
int main(void)
{
    int T = 0;
 
    scanf("%d"&T);
 
    while(T--)
    {
        scanf("%lf %lf %c %lf"&A, &B, &now, &goal);
 
        int MAX = 6;
 
        A *= 1000;
        B *= 1000;
        goal *= 1000;
 
        int cnt1 = 0;
        int cnt2 = 0;
 
        if (now == 'A'++cnt2; // A/B버튼을 눌러서 B로 이동하는 경우의 수 
        else if (now == 'B'++cnt1; // A/B버튼을 눌러서 A로 이동하는 경우의 수
 
        // A에서 출발
        // (now가 A일 때, A/B버튼을 안 누른 경우)
        // (now가 B일 때, A/B버튼을 누른 경우)
 
        int a = (int)A;
        int b = (int)A;
        int g = (int)goal;
 
        for (int i = 0; i < 6; i++)
        {
            if (a == g)
            {
                MAX = min(MAX, cnt1);
            }
 
            a += 20;
 
            if (a > 146000) a = 144000;
 
            if (b == g)
            {
                MAX = min(MAX, cnt1);
            }
 
            b -= 20;
 
            if (b < 144000) b = 146000;
 
            ++cnt1;
        }
 
        // B에서 출발
        // (now가 B일 때, A/B버튼을 안 누른 경우)
        // (now가 A일 때, A/B버튼을 누른 경우)
        
        a = (int)B;
        b = (int)B;
        g = (int)goal;
 
        for (int i = 0; i < 6; i++)
        {
            if (a == g)
            {
                MAX = min(MAX, cnt2);
            }
 
            a += 20;
 
            if (a > 146000) a = 144000;
 
            if (b == g)
            {
                MAX = min(MAX, cnt2);
            }
 
            b -= 20;
 
            if (b < 144000) b = 146000;
 
            ++cnt2;
        }
 
        printf("%d\n", MAX);
    }
 
    return 0;
}
cs





728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

2565번 전깃줄  (0) 2018.11.28
16472번 고냥이  (0) 2018.11.27
16471번 작은 숫자 내기  (0) 2018.11.26
15740번 A+B - 9  (2) 2018.11.26
16459번 독서의 계절  (0) 2018.11.26