-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path[BOJ] 14891 톱니바퀴.cpp
More file actions
133 lines (101 loc) · 2.36 KB
/
[BOJ] 14891 톱니바퀴.cpp
File metadata and controls
133 lines (101 loc) · 2.36 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <queue>
#include <string>
#define SIZE 8
using namespace std;
int wheel[4][8];
queue<pair<int, int>> q;
int K;
void shiftRight(int i) {
int temp;
temp = wheel[i][SIZE - 1];
for (int j = SIZE - 1; j > 0; j--) {
wheel[i][j] = wheel[i][j-1];
}
wheel[i][0] = temp;
}
void shiftLeft(int i) {
int temp;
temp = wheel[i][0];
for (int j = 0; j < SIZE - 1; j++) {
wheel[i][j] = wheel[i][j+1];
}
wheel[i][SIZE - 1] = temp;
}
void checkLeft(int idx, int direction) {
int i = idx;
while(idx > 0) {
if (wheel[idx][6] != wheel[idx-1][2]) idx--;
else break;
}
for (int j = i - 1; j >= idx; j--) {
if (direction == 1) {
shiftLeft(j);
direction = -1;
}
else if (direction == -1) {
shiftRight(j);
direction = 1;
}
}
}
void checkRight(int idx, int direction) {
int i = idx;
while(idx < 3) {
if (wheel[idx][2] != wheel[idx + 1][6]) idx++;
else break;
}
for (int j = i + 1; j <= idx; j++) {
if (direction == 1) {
shiftLeft(j);
direction = -1;
}
else if (direction == -1) {
shiftRight(j);
direction = 1;
}
}
}
void move() {
while(!q.empty()) {
if (1 == q.front().second) {
checkLeft(q.front().first, q.front().second);
checkRight(q.front().first, q.front().second);
shiftRight(q.front().first);
}
else if (-1 == q.front().second) {
checkLeft(q.front().first, q.front().second);
checkRight(q.front().first, q.front().second);
shiftLeft(q.front().first);
}
q.pop();
}
}
int score() {
int sum = 0;
if (wheel[0][0] == 1) sum += 1;
if (wheel[1][0] == 1) sum += 2;
if (wheel[2][0] == 1) sum += 4;
if (wheel[3][0] == 1) sum += 8;
return sum;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i = 0; i < 4; i++) {
string s;
cin >> s;
for (int j = 0; j < s.size(); j++) {
wheel[i][j] = s[j] - '0';
}
}
cin >> K;
for (int i = 0; i < K; i++) {
int n, m;
cin >> n >> m;
q.push({ n-1, m });
}
move();
cout << score();
return 0;
}