Game Develop

[Algorithm] Baekjoon 14891번 : 톱니바퀴 본문

Algorithm/Baekjoon

[Algorithm] Baekjoon 14891번 : 톱니바퀴

MaxLevel 2024. 7. 16. 16:25

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
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
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_map>
#include <stack>
#include <numeric>
#include <climits>
#include <bitset>
#include <cmath>
 
using namespace std;
 
 
int gears[4][8];
int gearOffets[4= { 0 };
int k, gearNum, dir;
int scores[4= { 1,2,4,8 };
 
// 2가 오른쪽, 6이 왼쪽.
// true 시계, false 이 반시계
 
int GetPoleIndex(int gearIndex, int poleIndex)
{
    int index = poleIndex - gearOffets[gearIndex];
    index %= 8;
 
    if (index < 0)
    {
        index = 8 + index;
    }
    else if (index > 7)
    {
        index = index - 8;
    }
 
    return index;
}
 
void RotateGear(int gearIndex, int prevGearNum, bool rotationDir)
{
    int fsad = 0;
 
    if (gearIndex - 1 >= 0 && gearIndex - 1 != prevGearNum)
    {
        // 왼쪽꺼회전.
        int curGearLeftPoleIndex = GetPoleIndex(gearIndex, 6);
        int leftGearRightPoleIndex = GetPoleIndex(gearIndex - 12);
        
        int curLeft = gears[gearIndex][curGearLeftPoleIndex];
        int leftGearRight = gears[gearIndex - 1][leftGearRightPoleIndex];
 
        if (curLeft != leftGearRight)
        {
            RotateGear(gearIndex - 1, gearIndex, !rotationDir);
        }
    }
 
    if (gearIndex + 1 <= 3 && gearIndex + 1 != prevGearNum)
    {
        // 오른쪽꺼회전.
 
        int curGearRightPoleIndex = GetPoleIndex(gearIndex, 2);
        int rightGearLeftPoleIndex = GetPoleIndex(gearIndex + 16);
 
        int curRight = gears[gearIndex][curGearRightPoleIndex];
        int rightGearLeft = gears[gearIndex + 1][rightGearLeftPoleIndex];
 
        if (curRight != rightGearLeft)
        {
            RotateGear(gearIndex + 1, gearIndex, !rotationDir);
        }
    }
 
    // 회전 후 적용.
    if (rotationDir) ++gearOffets[gearIndex];
    else --gearOffets[gearIndex];
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    for (int i = 0; i < 4++i)
    {
        string s;
        cin >> s;
 
        for (int j = 0; j < 8++j)
        {
            gears[i][j] = s[j] - '0';
        }
    }
 
    cin >> k;
 
    for (int i = 0; i < k; ++i)
    {
        cin >> gearNum >> dir;
 
        dir += 1;
        RotateGear(gearNum-15, dir);
    }
 
    int answer = 0;
 
    for (int i = 0; i < 4++i)
    {
        int poleIndex = GetPoleIndex(i, 0);
 
        if (gears[i][poleIndex] == 1)
        {
            answer += scores[i];
        }
    }
 
    cout << answer;
}
 
 
cs

 

구현문제이다. 

그대로 구현하면 되는데, 주의할점은 회전반영을 맨 마지막에 해야한다는 것이다.

하드코딩으로도 풀수있지만... 일반화시키는게 더 실력향상에 도움될것이다.