본문 바로가기

백준 사이트 코딩 문제/삼성 전자 기출문제

백준 14891번: 톱니바퀴 (C++)

 

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

 

특별한 알고리즘이 없는 단순 구현, 시뮬레이션 문제이다. 자세한 설명은 코드 주석으로 달아놨다.

 

 

 

<전체적인 알고리즘>

1. 해당 톱니를 움직인다.

2. 왼쪽 톱니들을 확인하며 움직일 톱니들을 움직인다.

3. 오른쪽 톱니들을 확인하며 움직일 톱니들을 움직인다.

4. 점수들은 계산해준다.

 

 

<전체 코드>

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
#include<iostream>
#include<string>
#include<deque>
 
using namespace std;
 
deque<char> wheel[4];
 
void move_wheel(int id, int dir)  //id톱니 dir방향으로 움직이기
{
    if (dir == -1)  //반시계방향
    {
        wheel[id].push_back(wheel[id][0]);
        wheel[id].pop_front();
    }
    else   //시계방향
    {
        wheel[id].push_front(wheel[id][7]);
        wheel[id].pop_back();
    }
};
 
void play(int num, int dir)  //톱니 전체 움직이기
{
    char pre_pole = wheel[num][6];   //움직이기 전의 극
 
    int tmp_dir=dir;
    for (int i = num-1; i >= 0; i--)  //움직일 톱니의 왼쪽 톱니들 움직이기
    {
        tmp_dir *= (-1); 
        if (wheel[i][2] == pre_pole)break//극이 같으면 패스
 
        pre_pole = wheel[i][6];  //움직이기 전의 극 저장
        move_wheel(i, tmp_dir);   //톱니 움직이기
    }
 
    pre_pole = wheel[num][2];
    tmp_dir = dir;
    for (int i = num+1; i < 4; i++)   //움직일 톱니의 오른쪽 톱니들 움직이기
    {
        tmp_dir *= (-1);
        if (wheel[i][6] == pre_pole)break//극이 같으면 패스
 
        pre_pole = wheel[i][2];
        move_wheel(i, tmp_dir);
    }
    move_wheel(num, dir);  //해당 톱니 움직이기
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    for (int i = 0; i < 4; i++)
    {
        string str;
        cin >> str;
        for (int j = 0; j < str.size(); j++)
        {
            wheel[i].push_back(str[j]);
        }
    }
 
    int k;
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        int num, dir;
        cin >> num >> dir;
        play(num-1, dir);
    }
 
    int plus = 1;  //더할 값
    int total = 0;
    for (int i = 0; i < 4; i++)
    {  
        if (wheel[i][0] == '1')total += plus;
        plus *= 2;
    }
    cout << total << "\n";
 
    return 0;
}

 

 

 

궁금하신 점은 댓글에 남겨주시면 답변드리겠습니다.

반응형