움직일 톱니바퀴와 그 주변에 있는 톱니바퀴가 맞다아있는 부분을 찾아 회전할지 안할지 판단하면 된다. 톱니바퀴는 총 4개이므로 if문을 통해 케이스를 나눌 수 있다.
1번 톱니바퀴가 움직이는 경우
2번 톱니바퀴가 움직일지 아닐지 판단한다. 만약 2번이 움직인다면 3번도 판단, 3번이 움직인다면 4번도 판단한다.
2번 톱니바퀴가 움직이는 경우
1번 or 3번이 움직이는지 판단한다. 만약 3번이 움직인다면 4번도 판단한다.
3번 톱니바퀴가 움직이는 경우
2번 or 4번이 움직이는지 판단한다. 만약 2번이 움직인다면 1번도 판단한다.
4번 톱니바퀴가 움직이는 경우
3번 톱니바퀴가 움직일지 아닐지 판단한다. 만약 3번이 움직인다면 2번도 판단, 2번이 움직인다면 1번도 판단한다.
총 4개의 케이스로 나누어 풀이할 수 있다.
톱니바퀴가 영향을 받아 회전한다면 방향이 반대로 바뀌기 때문에 그 부분도 판단해주어야한다.
bool 변수(움직이는가?), int 변수(어떤 방향인가)를 톱니바퀴마다 하나씩 두어 판단해주었다.
//백준 14891, 톱니바퀴
#include <iostream>
#include <deque>
std::deque<int> o;
std::deque<int> t;
std::deque<int> th;
std::deque<int> f;
void input(){
std::string s;
std::cin >> s;
for(int i{0}; i<8; ++i){
o.push_back(s[i]-'0');
}
std::cin >> s;
for(int i{0}; i<8; ++i){
t.push_back(s[i]-'0');
}
std::cin >> s;
for(int i{0}; i<8; ++i){
th.push_back(s[i]-'0');
}
std::cin >> s;
for(int i{0}; i<8; ++i){
f.push_back(s[i]-'0');
}
}
void solve(int a, int b){
bool fO{false}, fT{false}, fTh{false}, fF{false};
int cO{0}, cT{0}, cTh{0}, cF{0};
if(a == 1){
fO = true;
cO = b;
if(o[2] != t[6]){
fT = true;
cT = cO == -1 ? 1 : -1;
if(t[2] != th[6]){
fTh = true;
cTh = cT == -1 ? 1 : -1;
if(th[2] != f[6]){
fF = true;
cF = cTh == -1 ? 1 : -1;
}
}
}
}
else if(a == 2){
fT = true;
cT = b;
if(o[2] != t[6]){
fO = true;
cO = cT == -1 ? 1 : -1;
}
if(t[2] != th[6]){
fTh = true;
cTh = cT == -1 ? 1 : -1;
if(th[2] != f[6]){
fF = true;
cF = cTh == -1 ? 1 : -1;
}
}
}
else if(a == 3){
fTh = true;
cTh = b;
if(th[2] != f[6]){
fF = true;
cF = cTh == -1 ? 1 : -1;
}
if(th[6] != t[2]){
fT = true;
cT = cTh == -1 ? 1 : -1;
if(o[2] != t[6]){
fO = true;
cO = cT == -1 ? 1 : -1;
}
}
}
else{
fF = true;
cF = b;
if(f[6] != th[2]){
fTh = true;
cTh = cF == -1 ? 1 : -1;
if(th[6] != t[2]){
fT = true;
cT = cTh == -1 ? 1 : -1;
if(t[6] != o[2]){
fO = true;
cO = cT == -1 ? 1 : -1;
}
}
}
}
if(fO){
if(cO == -1){
auto tmp = o.front(); o.pop_front();
o.push_back(tmp);
} //앞에꺼 빼서 뒤로 보내기
else{
auto tmp = o.back(); o.pop_back();
o.push_front(tmp);
}
}
if(fT){
if(cT == -1){
auto tmp = t.front(); t.pop_front();
t.push_back(tmp);
} //앞에꺼 빼서 뒤로 보내기
else{
auto tmp = t.back(); t.pop_back();
t.push_front(tmp);
}
}
if(fTh){
if(cTh == -1){
auto tmp = th.front(); th.pop_front();
th.push_back(tmp);
} //앞에꺼 빼서 뒤로 보내기
else{
auto tmp = th.back(); th.pop_back();
th.push_front(tmp);
}
}
if(fF){
if(cF == -1){
auto tmp = f.front(); f.pop_front();
f.push_back(tmp);
} //앞에꺼 빼서 뒤로 보내기
else{
auto tmp = f.back(); f.pop_back();
f.push_front(tmp);
}
}
}
int main (){
input();
int K;
std::cin >> K;
int a, b;
for(int i{0}; i<K; ++i){
std::cin >> a >> b;
solve(a, b);
}
int sum{0};
if(o.front() == 1) ++sum;
if(t.front() == 1) sum += 2;
if(th.front() == 1) sum +=4;
if(f.front() == 1) sum += 8;
std::cout << sum;
return 0;
}