๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

14891๋ฒˆ: ํ†ฑ๋‹ˆ๋ฐ”ํ€ด

์ฒซ์งธ ์ค„์— 1๋ฒˆ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ์ƒํƒœ, ๋‘˜์งธ ์ค„์— 2๋ฒˆ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ์ƒํƒœ, ์…‹์งธ ์ค„์— 3๋ฒˆ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ์ƒํƒœ, ๋„ท์งธ ์ค„์— 4๋ฒˆ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ƒํƒœ๋Š” 8๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , 12์‹œ๋ฐฉํ–ฅ๋ถ€ํ„ฐ

www.acmicpc.net

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ์‹œ๊ณ„ ํ˜น์€ ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ๊ณผ ์–‘ ์˜† ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์™€ ๋งž๋ฌผ๋ฆฐ ์ด๋นจ์˜ ์ž์„ ๊ทน์ด ์„œ๋กœ ๋‹ค๋ฅผ ๊ฒฝ์šฐ, ํ•ด๋‹น ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋“ค ๋˜ํ•œ ์žฌ๊ท€์ ์œผ๋กœ ํšŒ์ „์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€ ํ•œ ๋‹จ๊ณ„์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  1๋ฒˆ๋ถ€ํ„ฐ 4๋ฒˆ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๊ฐ ์ด๋นจ์˜ ๊ทน ์ •๋ณด๋ฅผ ๋ฐฐ์—ด์—๋‹ค๊ฐ€ ์ €์žฅํ•ด์ค๋‹ˆ๋‹ค.

#define GEARWHELL_COUNT 4

vector<string> gearWheels(GEARWHELL_COUNT + 1);
for (int i = 1; i <= GEARWHELL_COUNT; i++)
    cin >> gearWheels[i];

 

๊ทธ ํ›„, ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ํšŒ์ „์‹œํ‚ค๋Š” ๊ฐœ์ˆ˜์ธ K ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ, ํšŒ์ „์‹œํ‚ฌ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ๋ฒˆํ˜ธ์™€ ๋ฐฉํ–ฅ์„ ์ •๋ณด๋ฅผ ๋ฐ›์•„์ค๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋ฅผ ํ† ๋Œ€๋กœ, ์–‘ ์˜† ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋“ค์„ ์žฌ๊ท€์ ์œผ๋กœ ํšŒ์ „์‹œ์ผœ ๋‚˜๊ฐˆ ๊ฑด๋ฐ, ์ฃผ์˜ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ์ „์„ ์ง„ํ–‰ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋Š” ๋‹ค์‹œ ํšŒ์ „์‹œํ‚ค๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

 

์ด๋ฅผ ๊ตฌ๋ณ„ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด, ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ณ„๋„์˜ bool ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

int K;
cin >> K;

while (K--)
{
    int gearWheelNumber, rotationDirection;
    cin >> gearWheelNumber >> rotationDirection;

    vector<bool> visited(GEARWHELL_COUNT + 1, false);
    RotateGearWheels(gearWheels, visited, gearWheelNumber, rotationDirection);
}

 

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ ๊ตฌํ˜„ ํ•จ์ˆ˜์ธ RotateGearWheels()๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๊ฐ ์ด๋นจ์€ 12์‹œ ๋ฐฉํ–ฅ์— ์žˆ๋Š” ์ด๋นจ์„ ๊ธฐ์ค€์œผ๋กœ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 0, 1, 2, ..., 7 ์ˆœ์„œ๋Œ€๋กœ ์ธ๋ฑ์Šค๋ฅผ ๋งค๊ฒจ๋†“์•˜์Šต๋‹ˆ๋‹ค.

#define LEFT_INTERLOCKING_TOOTH 6
#define RIGHT_INTERLOCKING_TOOTH 2
#define GEARWHELL_COUNT 4
#define DIRECTION_COUNT 8

void RotateGearWheels(vector<string>& gearWheels, vector<bool>& visited, const int gearId, const int direction)
{
    visited[gearId] = true;
    int leftGearWheelId = gearId - 1, rightGearWheelId = gearId + 1;

    if (1 <= leftGearWheelId and !visited[leftGearWheelId])
    {
        if (gearWheels[leftGearWheelId][RIGHT_INTERLOCKING_TOOTH] != gearWheels[gearId][LEFT_INTERLOCKING_TOOTH])
            RotateGearWheels(gearWheels, visited, leftGearWheelId, -direction);
    }

    if (rightGearWheelId <= 4 and !visited[rightGearWheelId])
    {
        if (gearWheels[rightGearWheelId][LEFT_INTERLOCKING_TOOTH] != gearWheels[gearId][RIGHT_INTERLOCKING_TOOTH])
            RotateGearWheels(gearWheels, visited, rightGearWheelId, -direction);
    }
    
    // ...
}

 

ํฌ๊ฒŒ ์–ด๋ ค์šด ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๊ธฐ์—, ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜์‹ค ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. 1)ํ˜„์žฌ ๋ฐฉ๋ฌธํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•œ ํ›„, 2)์–‘ ์˜† ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•ด, 3)ํšŒ์ „์‹œ์ผœ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ผ ๊ฒฝ์šฐ์—๋งŒ ํ•ด๋‹น ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋“ค์„ ํšŒ์ „์‹œํ‚ค๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค.

 

ํšŒ์ „์‹œ์ผœ์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋ž€ ์™ผ์ชฝ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ 2๋ฒˆ ์ด๋นจ๊ณผ ๋‚ด ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ 6๋ฒˆ ์ด๋นจ์˜ ๊ทน์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ, ์˜ค๋ฅธ์ชฝ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ 6๋ฒˆ ์ด๋นจ๊ณผ ๋‚ด ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ 2๋ฒˆ ์ด๋นจ์˜ ๊ทน์ด ์„œ๋กœ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. ์ด ๋•Œ, ํšŒ์ „ํ•ด์•ผ ํ•˜๋Š” ์˜† ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋Š” ํ˜„์žฌ ๋‚ด ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๊ฐ€ ํšŒ์ „ํ•ด์•ผ ํ•˜๋Š” ๋ฐฉํ–ฅ์˜ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์ด๋ฏ€๋กœ, ์Œ์ˆ˜(-) ๋ถ€ํ˜ธ๋ฅผ ๋ถ™์—ฌ์„œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์œ„ ๋กœ์ง์„ ํ†ตํ•ด ๋” ์ด์ƒ ์–‘ ์˜†์— ํšŒ์ „ํ•  ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๊ฐ€ ์—†๋Š” ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๊นŒ์ง€ ์™”์„ ๊ฒฝ์šฐ, ์ด์ œ ํšŒ์ „์„ ์‹œ์ž‘ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

#define LEFT_INTERLOCKING_TOOTH 6
#define RIGHT_INTERLOCKING_TOOTH 2
#define GEARWHELL_COUNT 4
#define DIRECTION_COUNT 8

void RotateGearWheels(vector<string>& gearWheels, vector<bool>& visited, const int gearId, const int direction)
{
    // ...
    
    int rotationCount = 0, toothId = 0;
    char previousMagneticPole = gearWheels[gearId][toothId];
    char temp;

    while (rotationCount < DIRECTION_COUNT)
    {
        int nextToothId = (toothId + DIRECTION_COUNT + direction) % DIRECTION_COUNT;
        temp = gearWheels[gearId][nextToothId];

        gearWheels[gearId][nextToothId] = previousMagneticPole;
        previousMagneticPole = temp;
        toothId = nextToothId;
        rotationCount++;
    }
}

 

K ๋ฒˆ ํšŒ์ „์„ ๋‹ค ํ–ˆ๋‹ค๋ฉด, 1๋ฒˆ ~ 4๋ฒˆ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ 0๋ฒˆ(12์‹œ ๋ฐฉํ–ฅ)์ด S๊ทน์ผ ๊ฒฝ์šฐ์—๋งŒ ์ ์ˆ˜๋ฅผ ๋งค๊ฒจ์„œ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

#define S '1'

int CalculatedPoint(const vector<string>& gearWheels)
{
    int totalPoint = 0;
    for (int i = 1; i <= 4; i++)
        if (gearWheels[i][0] == S)
            totalPoint += 1 << (i - 1);

    return totalPoint;
}

 

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <string>

using namespace std;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define S '1'
#define LEFT_INTERLOCKING_TOOTH 6
#define RIGHT_INTERLOCKING_TOOTH 2
#define GEARWHELL_COUNT 4
#define DIRECTION_COUNT 8

int CalculatedPoint(const vector<string>& gearWheels)
{
    int totalPoint = 0;
    for (int i = 1; i <= 4; i++)
        if (gearWheels[i][0] == S)
            totalPoint += 1 << (i - 1);

    return totalPoint;
}

void RotateGearWheels(vector<string>& gearWheels, vector<bool>& visited, const int gearId, const int direction)
{
    visited[gearId] = true;
    int leftGearWheelId = gearId - 1, rightGearWheelId = gearId + 1;

    if (1 <= leftGearWheelId and !visited[leftGearWheelId])
    {
        if (gearWheels[leftGearWheelId][RIGHT_INTERLOCKING_TOOTH] != gearWheels[gearId][LEFT_INTERLOCKING_TOOTH])
            RotateGearWheels(gearWheels, visited, leftGearWheelId, -direction);
    }

    if (rightGearWheelId <= 4 and !visited[rightGearWheelId])
    {
        if (gearWheels[rightGearWheelId][LEFT_INTERLOCKING_TOOTH] != gearWheels[gearId][RIGHT_INTERLOCKING_TOOTH])
            RotateGearWheels(gearWheels, visited, rightGearWheelId, -direction);
    }

    int rotationCount = 0, toothId = 0;
    char previousMagneticPole = gearWheels[gearId][toothId];
    char temp;

    while (rotationCount < DIRECTION_COUNT)
    {
        int nextToothId = (toothId + DIRECTION_COUNT + direction) % DIRECTION_COUNT;
        temp = gearWheels[gearId][nextToothId];

        gearWheels[gearId][nextToothId] = previousMagneticPole;
        previousMagneticPole = temp;
        toothId = nextToothId;
        rotationCount++;
    }
}

int main()
{
    FAST_IO

    vector<string> gearWheels(GEARWHELL_COUNT + 1);
    for (int i = 1; i <= GEARWHELL_COUNT; i++)
        cin >> gearWheels[i];

    int K;
    cin >> K;

    while (K--)
    {
        int gearWheelNumber, rotationDirection;
        cin >> gearWheelNumber >> rotationDirection;

        vector<bool> visited(GEARWHELL_COUNT + 1, false);
        RotateGearWheels(gearWheels, visited, gearWheelNumber, rotationDirection);
    }

    cout << CalculatedPoint(gearWheels);
    return 0;
}

 

728x90
๋ฐ˜์‘ํ˜•