백준 14891번 톱니바퀴

#14891. 톱니바퀴

Problem

Solution

  • SWEA 모의 SW 역량 테스트 [특이한 자석](https://www.notion.so/doyuni/4013-e51a7fc5e88b4e2b999dba66b24f358b#ffc1b4f8b7b140a2a1f2e12a9f953f5b) 와 동일하다.

1 Try

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
#include <cstdio>
#include <map>
using namespace std;
int magnatic[4][8];
map<int, int> task;
void rotate(int n, int dir) {
if (dir == 1) { // 시계 방향
int first_ele = magnatic[n][7];
for (int i = 6; i >= 0; --i) {
magnatic[n][i + 1] = magnatic[n][i];
}
magnatic[n][0] = first_ele;
}
else if (dir == -1) { // 반시계 방향
int last_ele = magnatic[n][0];
for (int i = 1; i < 8; ++i) {
magnatic[n][i - 1] = magnatic[n][i];
}
magnatic[n][7] = last_ele;
}
}
void checkRotate(int current_num, int prior_num, int dir) {
int count_dir = dir > 0 ? -1 : 1;
task.insert({ current_num, dir });
if (current_num == 0) {
if (prior_num != 1) {
if (magnatic[current_num][2] != magnatic[current_num + 1][6]) {
checkRotate(current_num + 1, current_num, count_dir);
}
}
}
else if (current_num == 1) {
if (prior_num != 0) {
if (magnatic[current_num - 1][2] != magnatic[current_num][6]) {
checkRotate(current_num - 1, current_num, count_dir);
}
}
if (prior_num != 2) {
if (magnatic[current_num][2] != magnatic[current_num + 1][6]) {
checkRotate(current_num + 1, current_num, count_dir);
}
}
}
else if (current_num == 2) {
if (prior_num != 1) {
if (magnatic[current_num - 1][2] != magnatic[current_num][6]) {
checkRotate(current_num - 1, current_num, count_dir);
}
}
if (prior_num != 3) {
if (magnatic[current_num][2] != magnatic[current_num + 1][6]) {
checkRotate(current_num + 1, current_num, count_dir);
}
}
}
else if (current_num == 3) {
if (prior_num != 2) {
if (magnatic[current_num - 1][2] != magnatic[current_num][6]) {
checkRotate(current_num - 1, current_num, count_dir);
}
}
}
}
int getScore() {
int ans = 0, score = 1;
for (int n = 0; n < 4; ++n) {
if (magnatic[n][0] == 1) {
ans += score;
}
score *= 2;
}
return ans;
}
int main() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 8; ++j) {
scanf("%1d", &magnatic[i][j]);
}
}
int k; scanf("%d", &k);
for (int i = 0; i < k; ++i) {
int number, dir; scanf("%d %d", &number, &dir);
checkRotate(number - 1, -1, dir);
for (auto e : task) {
rotate(e.first, e.second);
}
task.clear();
}
printf("%d\n", getScore());
return 0;
}