-
Notifications
You must be signed in to change notification settings - Fork 0
/
원판돌리기.cpp
111 lines (104 loc) · 3.22 KB
/
원판돌리기.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
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
// boj # 17822 원판 돌리기 (solved)
// 전형적인 시뮬레이션 문제! 어렵지는 않았지만 런타임 에러를 찾는 데 애먹었다.
// 런타임 에러 이유: int 함수 선언해놓고 리턴하지 않음
#include <cstring>
#include <deque>
#include <iostream>
#include <set>
using namespace std;
int N, M, T;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
deque<int> board[51];
bool candidate[51][51];
void rotate(int x, int d, int k) {
int tmp;
for (int i = x; i <= N; i += x) {
if (d == 0) // 시계방향 회전
for (int j = 0; j < k; j++) {
tmp = board[i].back();
board[i].pop_back();
board[i].push_front(tmp);
}
else // 반시계방향 회전
for (int j = 0; j < k; j++) {
tmp = board[i].front();
board[i].pop_front();
board[i].push_back(tmp);
}
}
}
bool checkAdjacent() {
bool hasAdjacent = false;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] == 0) continue;
for (int k = 0; k < 4; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 1 || nx > N) continue;
// 링의 첫 번째 숫자랑 M번째 숫자가 맞닿는 경우
if (ny < 0) ny = M - 1;
if (ny == M) ny = 0;
// 숫자가 같다면 지울 후보에 적어주기.
if (!candidate[nx][ny] && board[i][j] == board[nx][ny]) {
candidate[nx][ny] = true;
candidate[i][j] = true;
hasAdjacent = true;
}
}
}
}
return hasAdjacent;
}
void removeAdjacent() {
for (int i = 1; i <= N; i++)
for (int j = 0; j < M; j++)
if (candidate[i][j])
board[i][j] = 0;
}
void adjustNum() {
int sum = 0, cnt = 0;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] > 0) {
sum += board[i][j];
cnt += 1;
}
}
}
if (cnt == 0) return;
double avg = (double)sum / (double)cnt;
for (int i = 1; i <= N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] > 0) {
if ((double)board[i][j] > avg)
board[i][j] -= 1;
else if ((double)board[i][j] < avg)
board[i][j] += 1;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int tmp, x, d, k, answer = 0;
cin >> N >> M >> T;
for (int i = 1; i <= N; i++)
for (int j = 0; j < M; j++) {
cin >> tmp;
board[i].push_back(tmp);
}
while (T--) {
cin >> x >> d >> k;
memset(candidate, false, sizeof(candidate));
rotate(x, d, k);
if (checkAdjacent())
removeAdjacent();
else
adjustNum();
}
for (int i = 1; i <= N; i++)
for (int j = 0; j < M; j++)
answer += board[i][j];
cout << answer;
}