ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] BOJ 14891번: 톱니바퀴
    프로그래밍/알고리즘 PS 2020. 11. 13. 10:34

    문제 링크

    문제 풀이

    문제에 나온 조건에 따라 시뮬레이션 하면 풀 수 있는 간단한 문제이다. 1번부터 4번 톱니바퀴의 움직이기 전의 N극과 S극을 비교하여 어떤 톱니바퀴가 움직일지를 판단하고, 해당 톱니바퀴들을 움직이는 시뮬레이션 하는 방식으로 문제를 풀어주었다. 문제를 풀 때에 시계 방향과 반시계 방향을 코드로 작성하는 과정에서 반대로 생각하여 애를 먹었었다. 시계 방향과 반시계 방향을 헷갈리지 않게 조심해야겠다.

    코드

    #include <iostream>
    
    using namespace std;
    
    #define N '0'
    #define S '1'
    
    #define CLOCKWISE 1
    #define COUNTER_CLOCKWISE -1
    
    string wheel[5];
    
    int oldStartIdx[5] = {0, 0, 0, 0, 0};
    int newStartIdx[5] = {0, 0, 0, 0, 0};
    
    int K;
    
    int idx;
    int first_direction;
    int direction;
    
    int main(void)
    {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
    
        for (int i = 1; i <= 4; i++)
            cin >> wheel[i];
        cin >> K;
    
        for (int i = 0; i < K; i++)
        {
            cin >> idx >> first_direction;
            switch (first_direction)
            {
            case CLOCKWISE:
                newStartIdx[idx] = (oldStartIdx[idx] + 7) % 8;
                break;
            case COUNTER_CLOCKWISE:
                newStartIdx[idx] = (oldStartIdx[idx] + 1) % 8;
                break;
            }
    
            switch (first_direction)
            {
            case COUNTER_CLOCKWISE:
                direction = CLOCKWISE;
                break;
            case CLOCKWISE:
                direction = COUNTER_CLOCKWISE;
                break;
            }
    
            for (int j = idx; j > 1; j--)
            {
                if (wheel[j][(oldStartIdx[j] + 6) % 8] != wheel[j - 1][(oldStartIdx[j - 1] + 2) % 8])
                {
                    switch (direction)
                    {
                    case COUNTER_CLOCKWISE:
                        newStartIdx[j - 1] = (oldStartIdx[j - 1] + 1) % 8;
                        direction = CLOCKWISE;
                        break;
                    case CLOCKWISE:
                        newStartIdx[j - 1] = (oldStartIdx[j - 1] + 7) % 8;
                        direction = COUNTER_CLOCKWISE;
                        break;
                    }
                }
                else
                    break;
            }
            switch (first_direction)
            {
            case COUNTER_CLOCKWISE:
                direction = CLOCKWISE;
                break;
            case CLOCKWISE:
                direction = COUNTER_CLOCKWISE;
                break;
            }
    
            for (int j = idx; j < 4; j++)
            {
                if (wheel[j][(oldStartIdx[j] + 2) % 8] != wheel[j + 1][(oldStartIdx[j + 1] + 6) % 8])
                {
                    switch (direction)
                    {
                    case COUNTER_CLOCKWISE:
                        newStartIdx[j + 1] = (oldStartIdx[j + 1] + 1) % 8;
                        direction = CLOCKWISE;
                        break;
                    case CLOCKWISE:
                        newStartIdx[j + 1] = (oldStartIdx[j + 1] + 7) % 8;
                        direction = COUNTER_CLOCKWISE;
                        break;
                    }
                }
                else
                    break;
            }
            for (int j = 1; j <= 4; j++)
                oldStartIdx[j] = newStartIdx[j];
        }
    
        int result = 0;
    
        if (wheel[1][newStartIdx[1]] == S)
            result += 1;
        if (wheel[2][newStartIdx[2]] == S)
            result += 2;
        if (wheel[3][newStartIdx[3]] == S)
            result += 4;
        if (wheel[4][newStartIdx[4]] == S)
            result += 8;
    
        std::cout << result << '\n';
    
        return 0;
    }
Designed by Tistory.