Muscardinus
원판 돌리기 본문
728x90
#include <iostream>
using namespace std;
int board[50][50];
int n, m, t;
int answer = 0;
const int DEL = -1;
int dy[] = { -1,1,0,0 };
int dx[] = { 0,0,-1,1 };
void turn(int x, int d, int k) {
int pick = x - 1;
if (d == 1) k *= -1; //반시계
//회전
while (pick < n) {
int temp[50] = { 0, };
for (int i = 0; i < m; i++) {
temp[(i+k+m)%m] = board[pick][i];
}
for (int i = 0; i < m; i++) board[pick][i] = temp[i];
pick += x;
}
bool is_update = false;
int check[50][50] = { 0, };
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
for (int dir = 0; dir < 4; dir++) {
int ny = y + dy[dir];
int nx = (x + dx[dir] + m) % m;
if (ny < 0 || ny >= n) continue;
if (board[y][x] != DEL && board[ny][nx] != DEL && board[y][x] == board[ny][nx]) {
is_update = true;
check[y][x] = 1;
check[ny][nx] = 1;
}
}
}
}
// 수정
if (is_update) {
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (check[y][x]) board[y][x] = DEL;
}
}
}
//수정 x 평균으로 업데이트
else {
int sum = 0;
int count = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] != DEL) {
sum += board[y][x];
count++;
}
}
}
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] != DEL) {
if (board[y][x] * count > sum) board[y][x]--;
else if (board[y][x] * count < sum) board[y][x]++;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> t;
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
cin >> board[y][x];
}
}
for (int i = 0; i < t; i++) {
int x, d, k;
cin >> x >> d >> k;
turn(x, d, k);
}
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x]!=DEL) answer += board[y][x];
}
}
cout << answer << "\n";
return 0;
}
728x90
Comments