17822 Samsung sw test
코드
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
134
135
136
137
#include <iostream>
#include <cstring>
#include <vector>
#define MAX_BOARD 50
#define MAX_TURN 50
using namespace std;
int board[MAX_BOARD][MAX_BOARD];
int numOfTurn, numOfBoard, numOfNum;
int turn[MAX_TURN][3]; //xi, 0 or 1, ki
int getSumOfBoardNum();
void rotateBoard(vector<int> rotate, int direction, int ki);
void updateBoard();
int main() {
cin >> numOfBoard >> numOfNum >> numOfTurn;
for (int i = 0; i < numOfBoard; i++) {
for (int j = 0; j < numOfNum; j++) {
cin >> board[i][j];
}
}
for (int i = 0; i < numOfTurn; i++) {
cin >> turn[i][0] >> turn[i][1] >> turn[i][2];
}
cout << getSumOfBoardNum();
}
int getSumOfBoardNum() {
for (int current = 0; current < numOfTurn; current++) {
int xi = turn[current][0];
vector<int> rotate;
for (int i = 0; i < numOfBoard; i++) {
if (((i + 1) % xi) == 0)
rotate.push_back(i);
}
rotateBoard(rotate, turn[current][1], turn[current][2]);
updateBoard();
}
int sum = 0;
for (int i = 0; i < numOfBoard; i++)
for (int j = 0; j < numOfNum; j++)
sum += board[i][j];
return sum;
}
void rotateBoard(vector<int> rotate, int direction, int ki) {
if (direction == 0) { //시계
for (int i = 0; i < rotate.size(); i++) {
for (int k = 0; k < ki; k++) {
int temp = board[rotate[i]][0];
for (int j = 1; j < numOfNum; j++) {
int temp2 = board[rotate[i]][j];
board[rotate[i]][j] = temp;
temp = temp2;
}
board[rotate[i]][0] = temp;
}
}
}
else if (direction == 1) {
for (int i = 0; i < rotate.size(); i++) {
for (int k = 0; k < ki; k++) {
int temp = board[rotate[i]][numOfNum-1];
for (int j = numOfNum-1; j >= 0; j--) {
int temp2 = board[rotate[i]][j];
board[rotate[i]][j] = temp;
temp = temp2;
}
board[rotate[i]][numOfNum-1] = temp;
}
}
}
}
void updateBoard() {
int tempBoard[MAX_BOARD][MAX_BOARD];
bool check = false;
for (int i = 0; i < numOfBoard; i++) {
if (board[i][0] == board[i][1] || board[i][0] == board[i][numOfNum - 1] ||
(i != 0 && board[i][0] == board[i - 1][0]) || (i != (numOfBoard - 1) && board[i][0] == board[i + 1][0])) {
tempBoard[i][0] = 0;
if(board[i][0] != 0) check = true;
}
else
tempBoard[i][0] = board[i][0];
for (int j = 1; j < numOfNum - 1; j++) {
if (board[i][j] == board[i][j + 1] || board[i][j] == board[i][j - 1] ||
(i != 0 && board[i][j] == board[i - 1][j]) || (i != (numOfBoard - 1) && board[i][j] == board[i + 1][j])) {
tempBoard[i][j] = 0;
if(board[i][j] != 0) check = true;
}
else
tempBoard[i][j] = board[i][j];
}
if (board[i][numOfNum - 1] == board[i][0] || board[i][numOfNum - 1] == board[i][numOfNum - 2] ||
(i != 0 && board[i][numOfNum - 1] == board[i - 1][numOfNum - 1]) || (i != (numOfBoard - 1) && board[i][numOfNum - 1] == board[i + 1][numOfNum - 1])) {
tempBoard[i][numOfNum - 1] = 0;
if(board[i][numOfNum - 1] != 0) check = true;
}
else
tempBoard[i][numOfNum - 1] = board[i][numOfNum - 1];
}
if (!check) {
int sum = 0;
int count = 0;
for (int i = 0; i < numOfBoard; i++) {
for (int j = 0; j < numOfNum; j++) {
if (tempBoard[i][j] != 0) {
sum += tempBoard[i][j];
count++;
}
}
}
if (count != 0) {
float ave = (float)sum / (float)count;
for(int i = 0; i<numOfBoard; i++) {
for (int j = 0; j < numOfNum; j++) {
if (tempBoard[i][j] != 0) {
if (tempBoard[i][j] < ave)
tempBoard[i][j]++;
else if (tempBoard[i][j] > ave)
tempBoard[i][j]--;
}
}
}
}
}
memcpy(board, tempBoard, sizeof(tempBoard));
}