728x90
주어진 문제 그대로 구현하면 되는 시뮬레이션 문제입니다. 자석의 N과 S극이 닿게되면 같이 움직이므로 회전하는 방향이 바뀌는 것만 주의하면 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define endl '\n' | |
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); | |
using namespace std; | |
typedef long long ll; | |
typedef pair<int,int> pii; | |
typedef pair<ll, ll> pll; | |
typedef tuple<int,int,int> tiii; | |
const int INF=1e9+1; | |
void rotation(vector<vector<int>>& magnetic, int n, int dir) { // 1: CW, -1: CCW | |
// n번의 자석을 회전하는 함수 | |
if(dir==1) { | |
int tmp=magnetic[n][7]; | |
for(int k=7;k>=0;k--) magnetic[n][k]=magnetic[n][k-1]; | |
magnetic[n][0]=tmp; | |
} | |
else { | |
int tmp=magnetic[n][0]; | |
for(int k=0;k<8;k++) magnetic[n][k]=magnetic[n][k+1]; | |
magnetic[n][7]=tmp; | |
} | |
} | |
int solve() { | |
int k; cin>>k; | |
// magnetic[i][j]는 i번째 자석의 j번째 극성 | |
vector<vector<int>> magnetic(5,vector<int>(8)); | |
for(int i=1;i<=4;i++) for(auto& it : magnetic[i]) cin>>it; | |
while(k--) { | |
int n,dir; cin>>n>>dir; | |
vector<int> arr(5); | |
arr[n]=dir; | |
// 옆의 2번째 극성과 현재 자석의 6번째 극성이 다르다면 회전의 방향이 서로 달라진다 | |
// n-1번부터 1번 자석까지 영향 | |
for(int k=n;k>1;k--) { | |
if(magnetic[k-1][2]!=magnetic[k][6]) arr[k-1]=arr[k]*-1; | |
else break; | |
} | |
// n+1번부터 마지막 번호까지 영향 | |
for(int k=n;k<4;k++) { | |
if(magnetic[k][2]!=magnetic[k+1][6]) arr[k+1]=arr[k]*-1; | |
else break; | |
} | |
for(int k=1;k<5;k++) { | |
if(arr[k]) rotation(magnetic,k,arr[k]); | |
} | |
} | |
return magnetic[1][0]+magnetic[2][0]*2+magnetic[3][0]*4+magnetic[4][0]*8; | |
} | |
int main() { | |
fastio | |
int t; cin>>t; | |
for(int tc=1;tc<=t;tc++) { | |
cout<<'#'<<tc<<' '<<solve()<<'\n'; | |
} | |
return 0; | |
} |
728x90