8개의 톱니를 가진 톱니바퀴 T개가 일렬로 놓아져있다. 각 톱니는 N극 또는 S극을 가진다.
A톱니가 회전 시 맞닿은 B톱니의 극이 다르면 B톱니는 반대로 회전하고 극이 같으면 회전하지 않는다.
톱니의 초기상태와 회전시킬 톱니의 번호와 방향이 입력될 때 최종 톱니바퀴의 상태를 구하는 문제다.
isR
에 -1,0,1의 값을 넣는다.isR
에 따라 문자열 순서를 rotate
를 이용하여 시계방향 or 반시계방향으로 바꿔준다.#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int t, k, res, isR[1001];
queue<pair<int, int>> rot;
string s[1001];
void go()
{
for (int i = 0; i < t; i++)
{
if (isR[i] == 1)
rotate(s[i].begin(), s[i].begin() + s[i].size() - 1, s[i].end());
else if (isR[i] == -1)
rotate(s[i].begin(), s[i].begin() + 1, s[i].end());
}
}
int main()
{
cin >> t;
for (int i = 0; i < t; i++)
cin >> s[i];
cin >> k;
for (int i = 0; i < k; i++)
{
int num, dir;
cin >> num >> dir;
rot.push({num - 1, dir});
}
while (rot.size())
{
int cmd = rot.front().second;
int r = rot.front().first, l = rot.front().first;
isR[r] = cmd;
for (int i = rot.front().first - 1; i >= 0; i--)
{
if (!cmd)
{
isR[i] = 0;
continue;
}
if (s[r][6] == s[i][2])
{
isR[i] = 0;
cmd = 0;
}
else
{
cmd *= -1;
isR[i] = cmd;
r = i;
}
}
cmd = rot.front().second;
for (int i = rot.front().first + 1; i < t; i++)
{
if (!cmd)
{
isR[i] = 0;
continue;
}
if (s[i][6] == s[l][2])
{
isR[i] = 0;
cmd = 0;
}
else
{
cmd *= -1;
isR[i] = cmd;
l = i;
}
}
go();
rot.pop();
}
for (int i = 0; i < t; i++)
res += (s[i][0] - '0');
cout << res << "\n";
}