์‹ค๋ฒ„1 : ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

๋‹จ์ˆœ ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค. ๋น„ํŠธ๋งˆ์Šคํฌ ์—ฐ์Šต์„ ์œ„ํ•ด ๋น„ํŠธ๋งˆ์Šคํฌ๋กœ ํ’€์—ˆ๋‹ค. ํ•ด๋‹น ๊ณผ์ •์„ ํ•˜๋Š” ๋™์•ˆ์— ์ธ์ ‘ํ–‰๋ ฌ ๊ฐ™์€ matrix๋ฅผ ๋งŒ๋“ค์–ด ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•ด๋ณด๋ ค ํ–ˆ์ง€๋งŒ ์ข‹์ง€ ๋ชปํ–ˆ๋‹ค. ๊ตฌํ˜„ ๋ฌธ์ œ๋Š” ๋…ธ๊ฐ€๋‹ค๋กœ ์ ์–ด์ฃผ๋Š”๊ฒŒ ์ •์‹  ๊ฑด๊ฐ•์— ์ด๋กญ๋‹ค.

Pseudo-algorithm

  1. ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค.
  2. ์–ด๋–ค ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๋Œ๋ฆฌ๋Š” ์ง€ ํŒ๋‹จํ•œ๋‹ค.
  3. ๊ทธ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๋Œ๋ ธ์„ ๋•Œ, ๋”ฐ๋ผ์„œ ๋Œ์•„๊ฐ€๋Š” ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๋ฐฉํ–ฅ์„ ์ •ํ•œ๋‹ค.
  4. ๊ทธ ๊ตฌํ•œ ํ†ฑ๋‹ˆ ๋ฐ”ํ€ด์˜ ๋ฐฉํ–ฅ๋Œ€๋กœ ๋Œ๋ฆฐ๋‹ค.
  5. ๋งˆ์ง€๋ง‰์— ๋‹ต์„ ๊ตฌํ•œ๋‹ค.

ํŠน์ • ํ†ฑ๋‹ˆ๋ฐ”ํ€ด๋ฅผ ๋Œ๋ ธ์„ ๋•Œ, ๋”ฐ๋ผ์„œ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์˜ ์ˆซ์ž๋ฅผ 0์ด๋ฉด ๋Œ์•„๊ฐ€์ง€ ์•Š๋Š”๋‹ค. 1์ด๋ฉด ์‹œ๊ณ„๋ฐฉํ–ฅ, -1์ด๋ฉด ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ์ •ํ•˜์—ฌ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค.

vector<int> getAction(int gear, int dir){
    vector<int> action(5);
    if (gear == 1) {
        action[1] = dir;
        if (isRevolve(1, 2)) action[2] = -action[1];
        if (isRevolve(2, 3)) action[3] = -action[2];
        if (isRevolve(3, 4)) action[4] = -action[3];
    } else if (gear == 2) {
        action[2] = dir;
        if (isRevolve(1, 2)) action[1] = -action[2];
        if (isRevolve(2, 3)) action[3] = -action[2];
        if (isRevolve(3, 4)) action[4] = -action[3];
    } else if (gear == 3) {
        action[3] = dir;
        if (isRevolve(2, 3)) action[2] = -action[3];
        if (isRevolve(3, 4)) action[4] = -action[3];
        if (isRevolve(1, 2)) action[1] = -action[2];
    } else {
        action[4] = dir;
        if (isRevolve(3, 4)) action[3] = -action[4];
        if (isRevolve(2, 3)) action[2] = -action[3];
        if (isRevolve(1, 2)) action[1] = -action[2];
    }
    return action;
}

ํŠน์ • ํ†ฑ๋‹ˆ ๋ฐ”ํ€ด๊ฐ€ ๋Œ์•„๊ฐˆ ๋•Œ, ์ด์ „ ์ƒํƒœ๊ฐ€ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋ผ๋ฉด, ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด์˜ ๋ฐฉํ–ฅ์€ ๋ฌด์กฐ๊ฑด ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์ธ ๊ฒƒ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋˜ํ•œ ์ด์ „ ์ƒํƒœ์˜ ์–‘๊ทน์ด ๋ฐ˜๋Œ€๋ผ ํŠน์ • ํ†ฑ๋‹ˆ ๋ฐ”ํ€ด๊ฐ€ ๋Œ์•„๊ฐ€๊ฒŒ ๋˜๋ฉด ์ธ์ ‘ํ•œ ํ†ฑ๋‹ˆ๋ฐ”ํ€ด ์—ญ์‹œ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํŠน์ • ํ†ฑ๋‹ˆ๊ฐ€ ๋Œ์•„๊ฐ€์ง€ ์•Š์œผ๋ฉด 0์„ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ ์ธ์ ‘ ํ†ฑ๋‹ˆ๋„ ๋Œ์•„๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

์ฐธ, ๋น„ํŠธ๊ฐ€ ์ผœ์ ธ์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ๊ตฌ๋ถ„ํ•  ๋•Œ์—๋Š” ๊ผญ ๊ด„ํ˜ธ๋ฅผ ์จ์„œ ์ƒ๊ฐํ•˜์ž.

Code

#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<queue>
#include<functional>
using namespace std;
typedef long long ll;
int state[5];
int N, ans = 0;
 
bool isRevolve(int i, int j){
    if ((state[i] & 32 && state[j] & 2) || ((state[i] & 32) == 0 && (state[j] & 2) == 0)) {
        return false;
    }
    else return true;
}
 
void rotate(int gearNum, int way){
    if (way == 0) return;
    else if (way == 1) {
        int flag = state[gearNum] & 1;
        state[gearNum] = state[gearNum] >> 1;
        if (flag) state[gearNum] |= 128;
    } else {
        int flag = state[gearNum] & 128;
        state[gearNum] = state[gearNum] << 1;
        state[gearNum] &= ~256;
        if (flag) state[gearNum] |= 1;
    }
}
 
void getScore(){
    for (int i = 1; i <= 4; i++) {
        if (state[i] & 128) ans += int(pow(2, i-1));
    }
}
 
vector<int> getAction(int gear, int dir){
    vector<int> action(5);
    if (gear == 1) {
        action[1] = dir;
        if (isRevolve(1, 2)) action[2] = -action[1];
        if (isRevolve(2, 3)) action[3] = -action[2];
        if (isRevolve(3, 4)) action[4] = -action[3];
    } else if (gear == 2) {
        action[2] = dir;
        if (isRevolve(1, 2)) action[1] = -action[2];
        if (isRevolve(2, 3)) action[3] = -action[2];
        if (isRevolve(3, 4)) action[4] = -action[3];
    } else if (gear == 3) {
        action[3] = dir;
        if (isRevolve(2, 3)) action[2] = -action[3];
        if (isRevolve(3, 4)) action[4] = -action[3];
        if (isRevolve(1, 2)) action[1] = -action[2];
    } else {
        action[4] = dir;
        if (isRevolve(3, 4)) action[3] = -action[4];
        if (isRevolve(2, 3)) action[2] = -action[3];
        if (isRevolve(1, 2)) action[1] = -action[2];
    }
    return action;
}
 
void solve(int gear, int dir){
    vector<int> action = getAction(gear, dir);
    for (int i = 1; i <= 4; i++) rotate(i, action[i]);
}
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
 
    for (int i = 1; i <= 4; i++) {
        string s;
        cin >> s;
        for (int j = 7; j >= 0; j--) {
            int mul = int(pow(2, 7-j));
            int num = s[j]-'0';
            state[i] += num*mul;
        }
    }
 
    cin >> N;
    for (int i = 0; i < N; i++) {
        int gear, dir;
        cin >> gear >> dir;
        solve(gear, dir);
    }
    getScore();
    cout << ans << '\n';
 
    return 0;
}

Reference