문제 링크 : https://www.acmicpc.net/problem/14891


해결방법)

문제를 읽고나서 생각해 봣는데 각 명령마다 순차적으로 톱뉘를 돌려나가면 되겠다라고 생각했다.

즉, 단순구현으로 풀었고(소스가 더러운느낌,..) 톱니번호와 회전방향을 가지고 있는 그대로 코드를 짜보았다.


- 알고리즘 순서 

1. 톱니 바퀴 정보를 입력받는다.

2. 각 명령을 하기 직전에 톱니들 사이의 접합부분이 맞닿았는지 혹은 떨어졌는지를 확인한다

3. 명령마다 주어진 바퀴번호와 회전방향을 보면서 해당 바퀴번호 기준으로 접합정보를 차례대로 비교해 나가면서 바퀴를 회전 시켜준다.

4. 명령이 끝날때까지 2,3번을 반복하고 반복이 끝나면 답을 구해줄 수 있다.



소스코드) 

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
#include<iostream>
#include<cstring>
#define LEFT 6
#define RIGHT 2
using namespace std;
int cnt_cmd;
int wheel[4][8]; // 바퀴 0, 1, 2, 3
// 0 1 2 3 4 로 0은 12시방향 
int flag[4]; // 4바퀴의 회전여부 
void Check(int wheel[][8])
{
    if (wheel[0][RIGHT] == wheel[1][LEFT])
    {
        flag[0= -1;
    }
    else
    {
        flag[0= 1;
    }
    if (wheel[1][RIGHT] == wheel[2][LEFT])
    {
        flag[1= -1;
    }
    else
    {
        flag[1= 1;
    }
    if (wheel[2][RIGHT] == wheel[3][LEFT])
    {
        flag[2= -1;
    }
    else
        flag[2= 1;
 
}
void moveLeft(int wheel[][8] , int wheelnum)
{
    int tmp = wheel[wheelnum][RIGHT];
    for (int i = 3; i <= 7; i++)
    {
        wheel[wheelnum][i - 1= wheel[wheelnum][i];
    }
    wheel[wheelnum][7= wheel[wheelnum][0];
    wheel[wheelnum][0= wheel[wheelnum][1];
    wheel[wheelnum][1= tmp;
}
void moveRight(int wheel[][8] , int wheelnum)
{
    int tmp = wheel[wheelnum][LEFT];
    for (int i = 5; i >= 0; i--)
    {
        wheel[wheelnum][i + 1= wheel[wheelnum][i];
    }
    wheel[wheelnum][0= wheel[wheelnum][7];
    wheel[wheelnum][7= tmp;
}
int main()
{
    for (int j = 0; j < 8; j++)
    {
        scanf("%1d"&wheel[0][j]);
    }
    for (int j = 0; j < 8; j++)
    {
        scanf("%1d"&wheel[1][j]);
    }
    for (int j = 0; j < 8; j++)
    {
        scanf("%1d"&wheel[2][j]);
    }
    for (int j = 0; j < 8; j++)
    {
        scanf("%1d"&wheel[3][j]);
    }
    /*for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            printf("%d ", wheel[i][j]);
        }
        puts("");
    }*/
    cin >> cnt_cmd;
    int select, dir;
    int ret = 0;
    int tmp;
    for (int i = 0; i < cnt_cmd; i++)
    {
        scanf("%d %d"&select, &dir);
        memset(flag, 0sizeof(flag));
        // 한번의 명령 마다 12시 방향값들을 더해나간다.
 
        Check(wheel);
        if (select == 1)
        {
            // 1번 바퀴일때 
            if (dir == -1)
            {
                moveLeft(wheel, 0);
                if (flag[0== 1// 다르다면 회전한다.
                {
                    //반시계일때
                    moveRight(wheel, 1);
                    if (flag[1== 1)
                    {
                        moveLeft(wheel, 2);
                        if (flag[2== 1)
                        {
                            moveRight(wheel, 3);
                        }
                    }
                }
            }
            else if (dir == 1)
            {
                //시계일때
                moveRight(wheel, 0);
                if (flag[0== 1// 다르다면 회전한다.
                {
                    moveLeft(wheel, 1);
                    if (flag[1== 1)
                    {
                        moveRight(wheel, 2);
                        if (flag[2== 1)
                        {
                            moveLeft(wheel, 3);
                        }
                    }
                }
            }
        }
        else if (select == 2)
        {
            if (dir == -1)
            {
                moveLeft(wheel, 1); // 두번째바퀴 시계반대돌리고
                if (flag[0== 1)
                {
                    moveRight(wheel, 0); // 첫번째바퀴 시계방향돌린다.
                }
                if (flag[1== 1)
                {
                    moveRight(wheel, 2);
                    if (flag[2== 1)
                    {
                        moveLeft(wheel, 3);
                    }
                }
            }
            else if (dir == 1)
            {
                moveRight(wheel, 1);
                if (flag[0== 1)
                {
                    moveLeft(wheel, 0);
                }
                if (flag[1== 1)
                {
                    moveLeft(wheel, 2);
                    if (flag[2== 1)
                    {
                        moveRight(wheel, 3);
                    }
                }
            }
        }
 
 
        else if (select == 3)
        {
            if (dir == -1)
            {
                moveLeft(wheel, 2);
                if (flag[1== 1)
                {
                    //왼쪽이 달라서 돌릴수잇다면 
                    moveRight(wheel, 1);
                    if (flag[0== 1)
                    {
                        moveLeft(wheel, 0);
                    }
                }
                if (flag[2== 1)
                {
                    moveRight(wheel, 3);
                }
            }
            else if (dir == 1)
            {
                moveRight(wheel, 2);
                if (flag[1== 1)
                {
                    moveLeft(wheel, 1);
                    if (flag[0== 1)
                    {
                        moveRight(wheel, 0);
                    }
                }
                if (flag[2== 1)
                {
                    moveLeft(wheel, 3);
                }
            }
        }
        else if (select == 4)
        {
            if (dir == -1)
            {
                moveLeft(wheel, 3);
                if (flag[2== 1)
                {
                    moveRight(wheel, 2);
                    if (flag[1== 1)
                    {
                        moveLeft(wheel, 1);
                        if (flag[0== 1)
                        {
                            moveRight(wheel, 0);
                        }
                    }
                }
            }
            else if (dir == 1)
            {
                moveRight(wheel, 3);
                if (flag[2== 1)
                {
                    moveLeft(wheel, 2);
                    if (flag[1== 1)
                    {
                        moveRight(wheel, 1);
                        if (flag[0== 1)
                        {
                            moveLeft(wheel, 0);
                        }
                    }
                }
            }
        }
 
    }
    ret = 0;
    if (wheel[0][0== 1)ret += 1;
    if (wheel[1][0== 1)ret += 2;
    if (wheel[2][0== 1)ret += 4;
    if (wheel[3][0== 1)ret += 8;
 
    cout << ret << '\n';
}
cs


반응형

'PS > 백준' 카테고리의 다른 글

2448번 별 찍기 - 11  (0) 2018.11.12
1966번 프린터 큐  (0) 2018.10.28
7576번 토마토  (0) 2018.10.24
2839 설탕배달  (0) 2018.10.19
15686번 치킨 배달  (0) 2018.10.14

+ Recent posts