์ค๋ฒ1 : ๊ตฌํ ๋ฌธ์ ์ด๋ค.
์๊ฐ
๋จ์ ๊ตฌํ ๋ฌธ์ ์ด๋ค. ๋นํธ๋ง์คํฌ ์ฐ์ต์ ์ํด ๋นํธ๋ง์คํฌ๋ก ํ์๋ค. ํด๋น ๊ณผ์ ์ ํ๋ ๋์์ ์ธ์ ํ๋ ฌ ๊ฐ์ matrix๋ฅผ ๋ง๋ค์ด ๋ฌด์ธ๊ฐ๋ฅผ ํด๋ณด๋ ค ํ์ง๋ง ์ข์ง ๋ชปํ๋ค. ๊ตฌํ ๋ฌธ์ ๋ ๋ ธ๊ฐ๋ค๋ก ์ ์ด์ฃผ๋๊ฒ ์ ์ ๊ฑด๊ฐ์ ์ด๋กญ๋ค.
Pseudo-algorithm
- ์ ๋ ฅ์ ๋ฐ๋๋ค.
- ์ด๋ค ํฑ๋๋ฐํด๋ฅผ ๋๋ฆฌ๋ ์ง ํ๋จํ๋ค.
- ๊ทธ ํฑ๋๋ฐํด๋ฅผ ๋๋ ธ์ ๋, ๋ฐ๋ผ์ ๋์๊ฐ๋ ํฑ๋๋ฐํด์ ๋ฐฉํฅ์ ์ ํ๋ค.
- ๊ทธ ๊ตฌํ ํฑ๋ ๋ฐํด์ ๋ฐฉํฅ๋๋ก ๋๋ฆฐ๋ค.
- ๋ง์ง๋ง์ ๋ต์ ๊ตฌํ๋ค.
ํน์ ํฑ๋๋ฐํด๋ฅผ ๋๋ ธ์ ๋, ๋ฐ๋ผ์ ๋์๊ฐ๋ ๊ฒ์ ์ซ์๋ฅผ 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;
}