https://www.acmicpc.net/problem/14891
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int wheel[5][8];
int k;
void Rotate(vector<pair<int,int>> v) {
for (int i = 0; i < v.size(); i++) {
int turn = v[i].first;
int loc = v[i].second;
vector<int> tmp;
for (int j = 0; j < 8; j++) {
tmp.push_back(wheel[turn][j]);
}
if (loc == 1) {//시계
wheel[turn][0] = tmp[7];
for (int j = 1; j <= 7; j++) wheel[turn][j] = tmp[j - 1];
}
else if (loc == -1) { //반시계
wheel[turn][7] = tmp[0];
for (int j = 0; j < 7; j++) wheel[turn][j] = tmp[j + 1];
}
}
}
int main(void) {
for (int i = 1; i <= 4; i++) {
string s;
cin >> s;
for (int j = 0; j < 8; j++) {
wheel[i][j] = s[j] - '0';
}
}
cin >> k;
for (int i = 0; i < k; i++) {
int turn, loc; //1:시계, -1:반시계
cin >> turn >> loc;
vector<pair<int,int>> rot;
rot.push_back({ turn, loc });
if (turn == 1) {
if (wheel[turn][2] != wheel[turn + 1][6]) {
loc *= -1;
rot.push_back({ turn + 1, loc });
if (wheel[turn + 1][2] != wheel[turn + 2][6]) {
loc *= -1;
rot.push_back({ turn + 2,loc });
if (wheel[turn + 2][2] != wheel[turn + 3][6]) {
loc *= -1;
rot.push_back({ turn + 3,loc });
}
}
}
}
else if (turn == 2) {
int dir = loc;
if (wheel[turn][6] != wheel[turn - 1][2]) {
loc *= -1;
rot.push_back({ turn - 1,loc });
}
if (wheel[turn][2] != wheel[turn + 1][6]) {
dir *= -1;
rot.push_back({ turn + 1, dir });
if (wheel[turn + 1][2] != wheel[turn + 2][6]) {
dir *= -1;
rot.push_back({ turn + 2,dir });
}
}
}
else if (turn == 3) {
int dir = loc;
if (wheel[turn][6] != wheel[turn - 1][2]) {
loc *= -1;
rot.push_back({ turn - 1,loc });
if (wheel[turn - 1][6] != wheel[turn - 2][2]) {
loc *= -1;
rot.push_back({ turn - 2,loc });
}
}
if (wheel[turn][2] != wheel[turn + 1][6]) {
dir *= -1;
rot.push_back({ turn + 1,dir });
}
}
else if (turn == 4) {
if (wheel[turn][6] != wheel[turn - 1][2]) {
loc *= -1;
rot.push_back({ turn - 1, loc });
if (wheel[turn - 1][6] != wheel[turn - 2][2]) {
loc *= -1;
rot.push_back({ turn - 2,loc });
if (wheel[turn - 2][6] != wheel[turn - 3][2]) {
loc *= -1;
rot.push_back({ turn - 3,loc });
}
}
}
}
Rotate(rot);
}
//N:0, S:1
int answer = 0;
if (wheel[1][0] == 1) answer += 1;
if (wheel[2][0] == 1) answer += 2;
if (wheel[3][0] == 1) answer += 4;
if (wheel[4][0] == 1) answer += 8;
cout << answer << endl;
}