본문 바로가기

백준

14891)톱니바퀴

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


똑바로 글을 읽어야 한다는 걸 깨닫게 해준 문제였다.


톱니가 어떻게 회전하는 지를 잘 파악해야 한다.


첫 번째로 문제를 풀 때 톱니가 회전한 후, 양쪽의 톱니가 맞물리는 지 검사했다.


두 번쨰로 문제를 풀 땐 선택한 톱니가 회전하기 전 양쪽의 톱니가 맞물리는 지 검사하고 첫 번째 사용했던 코드를 그대로 가져다 썼다.


세 번째로 문제를 풀었을 때 제대로 비로소 문제를 이해하게 됬는데 =_=...


톱니가 회전하기 전에 톱니들 끼리 맞물렸을 때 극이 같은 지, 다른 지 검사하고 다른 경우 회전방향을  토글시킨다.


회전시키기 전에 각 톱니의 회전 방향을 배열에다가 저장해야 한다.


그리고 저장해둔 회전 방향에 따라 톱니를 회전시켜야 한다.


문제를 잘 읽자 ㅠㅠ


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
#include<stdio.h>
#define CW 1
#define CCW -1
#define GEAR_NUM 4
#define TOOTH_NUM 8
 
int toggle(int direction) {
    if (direction == CW) return CCW;
    else return CW;
}
 
void rotateCW(char gear[GEAR_NUM][TOOTH_NUM + 1], int select) {
    char temp;
    int cnt = TOOTH_NUM-1;
 
    temp = gear[select][TOOTH_NUM-1];
        
    while (cnt > 0) {
        gear[select][cnt] = gear[select][cnt-1];
        cnt--;
    }
    gear[select][0= temp;
}
 
void rotateCCW(char gear[GEAR_NUM][TOOTH_NUM + 1], int select) {
    char temp;
    int cnt = 0;
 
    temp = gear[select][0];
 
    while (cnt < TOOTH_NUM-1) {
        gear[select][cnt] = gear[select][cnt+1];
        cnt++;
    }
    gear[select][TOOTH_NUM-1= temp;
}
 
void rotate(char gear[GEAR_NUM][TOOTH_NUM+1], int select,int direction) {
    int i = select, j = select;
    int right = direction, left = direction;
    int eachDir[GEAR_NUM] = {NULL};
 
    eachDir[select] = direction;
    while (gear[i][2!= gear[i + 1][6&& i + 1 < GEAR_NUM) {
        right = toggle(right);
        eachDir[++i] = right;
    }
    while (gear[j][6!= gear[j - 1][2&& j - 1 >= 0) {
        left = toggle(left);
        eachDir[--j] = left;
    }
 
    for (int i = 0; i < GEAR_NUM;i++) {
        switch (eachDir[i]){
        case CW:
            rotateCW(gear, i);
            break;
        case CCW:
            rotateCCW(gear, i);
            break;
        }
    }
 
    for (int i = 0; i < GEAR_NUM; i++) {
        eachDir[i] = NULL;
    }
}
 
int main() {
    char gear[GEAR_NUM][TOOTH_NUM+1];
    int cntRotate;
    int selGear;
    int rotateState;
    int score = 0;
    int point = 1;
 
    for (int i = 0; i < GEAR_NUM;i++) {
        scanf("%s",gear[i]);
    }
    scanf("%d",&cntRotate);
    
    for (int i = 0; i < cntRotate;i++) {
        scanf("%d",&selGear);
        scanf("%d",&rotateState);
        selGear--;
 
        switch (rotateState){
            case CW:
                rotate(gear, selGear, CW);
                break;
            case CCW:
                rotate(gear, selGear, CCW);
                break;
        }
    }
 
    for (int i = 0; i < GEAR_NUM;i++) {
        score += (gear[i][0- '0')*point;
        point *= 2;
    }
    printf("%d\n",score);
 
    return 0;
}
cs



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

15501)부당한 퍼즐  (0) 2019.02.18
16396)선 그리기  (0) 2019.02.17
2583)영역 구하기  (0) 2019.02.16
10158)개미  (0) 2019.02.14
10159)저울  (0) 2019.02.13