14891번: 톱니바퀴
총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴
www.acmicpc.net
시뮬레이션
삼성 SW 기출
1. 문제 해결 아이디어
하나의 톱니가 돌아가면 다른 톱니도 돌아가야하므로 연쇄반응을 구현해야한다.
재귀로 풀어내려고 했고
현재 바퀴를 시계/반시계 방향으로 회전시키기 전의 3시, 9시 방향의 자기정보를 가지고 있어야 한다.
현재 영향을 주는 방향을 매개변수로 입력받아서(lr) (0은 양방향, 1은 오른쪽으로, -1은 왼쪽으로) 영향을 줄 바퀴를 정해서 함수를 호출했다.
로직은 이게 전부인듯 하다.
삼성 SW기출답게 구현력을 요구하는 문제이다. 1시간쯤 걸렸다.
이번 코드는 마음에 든다.
다른 분들의 코드를 보고 다시보니 맘에 들지않는다ㅋㅋ
2. 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#include <iostream>
#include <vector>
using namespace std;
struct Wheel{
int tooth[8];
};
// N극 0, S극 1
Wheel Saw[4];
int K;
vector<pair<int, int> > Rinfo;
void input(){
string str;
for(int i = 0; i < 4; i++){
cin >> str;
for(int k = 0; k < 8; k++){
Saw[i].tooth[k] = str[k] - '0';
}
}
int a, b;
cin >> K;
for(int i = 0; i < K; i++){
cin >> a >> b;
Rinfo.push_back({a, b});
}
}
void RotateWheel(int wheelnum, int dir, int lr){
int now3 = Saw[wheelnum].tooth[2];
int now9 = Saw[wheelnum].tooth[6];
int nextdir = (dir == 1? -1 : 1);
// **** 현재 바퀴 회전 ****
int temp[8];
if(dir == 1){
for(int i = 1; i <= 8; i++){
temp[i % 8] = Saw[wheelnum].tooth[i-1];
}
}
else if(dir == -1){
for(int i = 7; i >= 0; i--){
temp[(i+7) % 8] = Saw[wheelnum].tooth[i];
}
}
for(int i = 0; i < 8; i++){
Saw[wheelnum].tooth[i] = temp[i];
}
// **** 근처의 바퀴가 영향을 받는지 확인하고 회전시키는 함수 호출 ****
if(wheelnum == 0){
if(lr >= 0 && now3 != Saw[1].tooth[6]) RotateWheel(1, nextdir, 1);
}
else if(wheelnum == 1){
if(lr >= 0 && now3 != Saw[2].tooth[6]) RotateWheel(2, nextdir, 1);
if(lr <= 0 && now9 != Saw[0].tooth[2]) RotateWheel(0, nextdir, -1);
}
else if(wheelnum == 2){
if(lr >= 0 && now3 != Saw[3].tooth[6]) RotateWheel(3, nextdir, 1);
if(lr <= 0 && now9 != Saw[1].tooth[2]) RotateWheel(1, nextdir, -1);
}
else if(wheelnum == 3){
if(lr <= 0 && now9 != Saw[2].tooth[2]) RotateWheel(2, nextdir, -1);
}
}
void Solve(){
for(int i = 0; i < K; i++){
int wheelnum = Rinfo[i].first;
int dir = Rinfo[i].second;
RotateWheel(wheelnum-1, dir, 0);
}
int res = 0;
if(Saw[0].tooth[0]) res += 1;
if(Saw[1].tooth[0]) res += 2;
if(Saw[2].tooth[0]) res += 4;
if(Saw[3].tooth[0]) res += 8;
cout << res;
}
int main(){
input();
Solve();
return 0;
}
|
cs |
'Algorithm > BOJ' 카테고리의 다른 글
[백준 14003] 가장 긴 증가하는 부분 수열5 C++ (0) | 2021.07.08 |
---|---|
[백준 3055] 탈출 C++ (0) | 2021.07.07 |
[백준 6087] 레이저 통신 C++ (0) | 2021.07.06 |
[백준 14890] 경사로 C++ (0) | 2021.07.06 |
[백준 1238] 파티 C++ (1) | 2021.07.05 |