๐ŸŒฒ Altu-Bitu/๊ตฌํ˜„&์‹œ๋ฎฌ๋ ˆ์ด์…˜

[BOJ][C++] ๋ฐฑ์ค€ 14891๋ฒˆ: ํ†ฑ๋‹ˆ๋ฐ”ํ€ด

์„ ๋‹ฌ 2022. 9. 20. 13:20
๋ฐ˜์‘ํ˜•

[๐Ÿฃ EDOC/EPPER ๊ธฐ์ถœ (0908-0915)] - [BOJ G5][C++] ๋ฐฑ์ค€ 14891๋ฒˆ: ํ†ฑ๋‹ˆ๋ฐ”ํ€ด

 

[BOJ G5][C++] ๋ฐฑ์ค€ 14891๋ฒˆ: ํ†ฑ๋‹ˆ๋ฐ”ํ€ด

https://www.acmicpc.net/problem/14891 14891๋ฒˆ: ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ์ด 8๊ฐœ์˜ ํ†ฑ๋‹ˆ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ†ฑ๋‹ˆ๋ฐ”ํ€ด 4๊ฐœ๊ฐ€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ผ๋ ฌ๋กœ ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ๋˜, ํ†ฑ๋‹ˆ๋Š” N๊ทน ๋˜๋Š” S๊ทน ์ค‘ ํ•˜๋‚˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ๋‹ค. ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์—

whkakrkr.tistory.com

 

๋‚˜๋Š” ์ง„ํ™”ํ–ˆ๋‹ค.

 

// Authored by : seondal
// Co-authored by : -

// #include <bits/stdc++.h>
#include <iostream>
#include <deque>
#include <vector>
#include <cmath>

using namespace std;

// ๋งž๋‹ฟ๋Š” ํ†ฑ๋‹ˆ์˜ ์ธ๋ฑ์Šค
const int R = 2;
const int L = 6;

deque<int> wheel[5]; // ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ์ƒํƒœ
vector<int> rotation(5, 0);  // ์‹œ๊ณ„๋ฐฉํ–ฅ(1), ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ(-1), ํšŒ์ „์—†์Œ(0)

void decideRotation(int num, int rot) {
    if(rotation[num] != 0)
        return;  // ์ด๋ฏธ ํšŒ์ „ ๋ฐฉํ–ฅ ๊ฒฐ์ •์ด๋ผ๋ฉด ๋ฆฌํ„ด
    
    rotation[num] = rot;
    
    // ์˜ค๋ฅธ์ชฝ ๋ฐ”ํ€ด ๋ฐฉํ–ฅ ๊ฒฐ์ •
    if(num<4 && wheel[num][R] != wheel[num+1][L]) {
        decideRotation(num+1, -rot);
    }
    
    // ์™ผ์ชฝ ๋ฐ”ํ€ด ๋ฐฉํ–ฅ ๊ฒฐ์ •
    if(num>1 && wheel[num][L] != wheel[num-1][R]) {
        decideRotation(num-1, -rot);
    }
}

void rotate() {
    for(int i=1; i<=4; i++) {
        if(rotation[i] == 1) {
            // ์‹œ๊ณ„๋ฐฉํ–ฅ ํšŒ์ „
            int tmp = wheel[i].back();
            wheel[i].pop_back();
            wheel[i].push_front(tmp);
        } else if (rotation[i] == -1) {
            // ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „
            int tmp = wheel[i].front();
            wheel[i].pop_front();
            wheel[i].push_back(tmp);
        }
    }
    return;
}

int calcScore() {
    int answer = 0;
    for(int i=1; i<=4; i++) {
        answer += pow(2, i-1) * wheel[i][0];
    }
    return answer;
}

int main() {
    string temp;
    for(int i=1; i<=4; i++) {
        cin >> temp;
        for(int j=0; j<8; j++) {
            wheel[i].push_back(temp[j] - '0');
        }
    }
    
    int n, num, rot;
    cin >> n;
    
    while(n--) {
        cin >> num >> rot;
        rotation.assign(5, 0);
        
        // ํšŒ์ „ ๋ฐฉํ–ฅ ๊ฒฐ์ •
        decideRotation(num, rot);
        
        // ํšŒ์ „ํ•˜๊ธฐ
        rotate();
    }
    
    cout << calcScore();
    
    return 0;
}

/*
 */
๋ฐ˜์‘ํ˜•