-
Notifications
You must be signed in to change notification settings - Fork 0
/
14891 톱니바퀴(deque, queue, 구현).cpp
85 lines (80 loc) · 1.4 KB
/
14891 톱니바퀴(deque, queue, 구현).cpp
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
#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
#include<string>
#include<cstdlib>
using namespace std;
deque<int> gear[5];
void spin(int num, int dir) {
if (dir == 1) {
gear[num].push_front(gear[num].back());
gear[num].pop_back();
}
else {
gear[num].push_back(gear[num].front());
gear[num].pop_front();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
for (int i = 1; i < 5; i++) {
string s;
cin >> s;
for (int j = 0; j < 8; j++) {
gear[i].push_back(s[j] - '0');
}
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int num, dir;
cin >> num >> dir;
queue<pair<int, int>> q;
q.push({ num,dir });
int idx = num;
int tmp_dir = dir;
while (1) {
if (idx == 4) {
break;
}
else {
idx++;
tmp_dir *= -1;
if (gear[idx - 1][2]!=gear[idx][6]) {
q.push({ idx,tmp_dir });
}
else
break;
}
}
idx = num;
tmp_dir = dir;
while (1) {
if (idx == 1) {
break;
}
else {
idx--;
tmp_dir *= -1;
if (gear[idx][2]!=gear[idx + 1][6]) {
q.push({ idx,tmp_dir });
}
else
break;
}
}
while (q.size()) {
spin(q.front().first, q.front().second);
q.pop();
}
}
int res = 0;
for (int i = 1,j=1; i < 5; i++,j*=2) {
if (gear[i][0]) {
res += j;
}
}
cout << res << '\n';
}