출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

상세 컨텐츠

본문 제목

백준 14891 톱니바퀴(구현, 시뮬레이션)

알고리즘

by 장동균 2020. 8. 13. 23:04

본문

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

 

14891번: 톱니바퀴

총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴

www.acmicpc.net

#include <iostream>

using namespace std;
int circle[4][8];
bool check[4];
int dir[4];
void go()
{
    for (int i = 0; i < 4; i++)
    {
        if (check[i] == 1)
        {
            if (dir[i] == 1)
            {
                int tmp = circle[i][7];
                for (int j = 7; j >= 1; j--)
                {
                    circle[i][j] = circle[i][j - 1];
                }
                circle[i][0] = tmp;
            }
            else if (dir[i] == -1)
            {
                int tmp = circle[i][0];
                for (int j = 1; j < 8; j++)
                {
                    circle[i][j - 1] = circle[i][j];
                }
                circle[i][7] = tmp;
            }
        }
    }
}
void rotate(int number, int direction)
{
    if (number == 0)
    {
        check[0] = true;
        dir[0] = direction;
        if (circle[0][2] != circle[1][6])
        {
            check[1] = true;
            dir[1] = dir[0] * -1;
            if (circle[1][2] != circle[2][6])
            {
                check[2] = true;
                dir[2] = dir[1] * -1;
                if (circle[2][2] != circle[3][6])
                {
                    check[3] = true;
                    dir[3] = dir[2] * -1;
                }
            }
        }
        go();
    }
    else if (number == 1)
    {
        check[1] = true;
        dir[1] = direction;
        if (circle[1][6] != circle[0][2])
        {
            check[0] = true;
            dir[0] = dir[1] * -1;
        }
        if (circle[1][2] != circle[2][6])
        {
            check[2] = true;
            dir[2] = dir[1] * -1;
            if (circle[2][2] != circle[3][6])
            {
                check[3] = true;
                dir[3] = dir[2] * -1;
            }
        }
        go();
    }
    else if (number == 2)
    {
        check[2] = true;
        dir[2] = direction;
        if (circle[2][2] != circle[3][6])
        {
            check[3] = true;
            dir[3] = dir[2] * -1;
        }
        if (circle[1][2] != circle[2][6])
        {
            check[1] = true;
            dir[1] = dir[2] * -1;
            if (circle[0][2] != circle[1][6])
            {
                check[0] = true;
                dir[0] = dir[1] * -1;
            }
        }
        go();
    }
    else if (number == 3)
    {
        check[3] = true;
        dir[3] = direction;
        if (circle[2][2] != circle[3][6])
        {
            check[2] = true;
            dir[2] = dir[3] * -1;
            if (circle[1][2] != circle[2][6])
            {
                check[1] = true;
                dir[1] = dir[2] * -1;
                if (circle[0][2] != circle[1][6])
                {
                    check[0] = true;
                    dir[0] = dir[1] * -1;
                }
            }
        }
        go();
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    for (int i = 0; i < 4; i++)
    {
        string tmp;
        cin >> tmp;
        for (int j = 0; j < 8; j++)
        {
            circle[i][j] = tmp[j] - '0';
        }
    }
    int rotation;
    cin >> rotation;
    for (int i = 0; i < rotation; i++)
    {
        int number, direction;
        cin >> number >> direction;
        rotate(number - 1, direction);
        for (int j = 0; j < 4; j++)
        {
            dir[j] = 0;
            check[j] = false;
        }
    }
    int ans = 0;
    if (circle[0][0])
        ans += 1;
    if (circle[1][0])
        ans += 2;
    if (circle[2][0])
        ans += 4;
    if (circle[3][0])
        ans += 8;
    cout << ans;
    // for(int i=0;i<4;i++)
    // for(int j=0;j<8;j++)
    // cout<<circle[i][j];
}

뭐 어떤 식으로 풀어야겠다는 감이 잡히지 않아서 하드코딩으로 풀었다.

 

과연 이게 좋은 코드일까...........

관련글 더보기

댓글 영역