https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 101;
vector<int> map[MAX][MAX];
int n, m, k;
struct MOVE { int x, y; };
MOVE mv[4] = { {-1,0}, {1,0}, {0,-1}, {0,1} };
struct INFO { int x, y, num, dir; };
vector<INFO> cluster;
void MoveCluster() {
for (int i = 0; i < cluster.size(); i++) {
if (cluster[i].x == -1 && cluster[i].y == -1 && cluster[i].num == -1 && cluster[i].dir == -1) continue;
int ii = cluster[i].x;
int jj = cluster[i].y;
int dir = cluster[i].dir;
int num = cluster[i].num;
int movei = ii + mv[cluster[i].dir].x;
int movej = jj + mv[cluster[i].dir].y;
if (movei == 0 || movei == n - 1 || movej == 0 || movej == n - 1) {
if (cluster[i].num <= 1) {
cluster[i] = { -1,-1,-1,-1 };
for (int j = 0; j < map[ii][jj].size(); j++) {
if (map[ii][jj][j] == i) map[ii][jj].erase(map[ii][jj].begin() + j);
}
continue;
}
if (dir == 0) dir = 1;
else if (dir == 1) dir = 0;
else if (dir == 2) dir = 3;
else if (dir == 3) dir = 2;
num = num / 2;
}
cluster[i] = { movei,movej,num,dir };
for (int j = 0; j < map[ii][jj].size(); j++) {
if (map[ii][jj][j] == i) map[ii][jj].erase(map[ii][jj].begin() + j);
}
map[movei][movej].push_back(i);
}
}
void CheckSame() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (map[i][j].size() > 1) {
vector<int> del;
int biggest = -1;
int sum = 0;
int index = -1;
for (int k = 0; k < map[i][j].size(); k++) {
del.push_back(map[i][j][k]);
if (cluster[map[i][j][k]].num > biggest) {
biggest = cluster[map[i][j][k]].num;
index = map[i][j][k];
}
sum += cluster[map[i][j][k]].num;
}
int new_dir = cluster[index].dir;
for (int k = 0; k < del.size(); k++) {
cluster[del[k]] = { -1,-1,-1,-1 };
}
cluster[index] = { i,j,sum,new_dir };
map[i][j].clear();
map[i][j].push_back(index);
}
}
}
}
int main(void) {
int testcase;
cin >> testcase;
int index = 1;
for (; testcase > 0; testcase--) {
cin >> n >> m >> k;
int answer = 0;
cluster.clear();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
map[i][j].clear();
}
}
for (int i = 0; i < k; i++) {
int x, y, num, dir;
cin >> x >> y >> num >> dir;
cluster.push_back({ x,y,num,dir - 1 });
map[x][y].push_back(i);
}
for (; m > 0; m--) {
MoveCluster();
CheckSame();
}
for (int i = 0; i < cluster.size(); i++) {
if (cluster[i].x == -1 && cluster[i].y == -1 && cluster[i].num == -1 && cluster[i].dir == -1) continue;
answer += cluster[i].num;
}
cout << "#" << index << " " << answer << "\n";
index++;
}
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
탈주범 검거 C++ (0) | 2022.04.28 |
---|---|
점심 식사시간 C++ (0) | 2022.04.25 |
무선 충전 C++ (0) | 2022.04.25 |
보물상자 비밀번호 (0) | 2022.04.24 |
벽돌 깨기 C++ (0) | 2022.04.24 |