https://www.acmicpc.net/problem/21610
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 55;
int n, m;
int map[MAX][MAX];
int d, s;
int answer = 0;
struct MOVE { int x, y; };
MOVE mv[8] = { {0,-1}, {-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1} };
vector<pair<int, int>> cloud;
void printmap() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
}
void MoveCloud() {
for (int i = 0; i < cloud.size(); i++) {
int movei = cloud[i].first + mv[d].x * s;
int movej = cloud[i].second + mv[d].y * s;
while (movei <= 0) movei = (n + movei);
while (movei > n) movei = movei - n;
while (movej <= 0) movej = (n + movej);
while (movej > n) movej = movej - n;
cloud[i] = make_pair(movei, movej);
}
}
void PlusWater() {
for (int i = 0; i < cloud.size(); i++) {
map[cloud[i].first][cloud[i].second] += 1;
}
}
void Magic() {
for (int i = 0; i < cloud.size(); i++) {
int movei, movej;
int count = 0;
for (int k = 1; k < 8; k+=2) {
movei = cloud[i].first + mv[k].x;
movej = cloud[i].second + mv[k].y;
if (!(movei >= 1 && movei <= n && movej >= 1 && movej <= n)) continue;
if (map[movei][movej] > 0) count++;
}
map[cloud[i].first][cloud[i].second] += count;
}
}
void MakeCloud() {
vector<pair<int, int>> tmp;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (map[i][j] >= 2) {
if (find(cloud.begin(), cloud.end(), make_pair(i, j)) == cloud.end()) {
map[i][j] -= 2;
tmp.push_back({ i,j });
}
}
}
}
cloud = tmp;
}
int main(void) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> map[i][j];
}
}
cloud.push_back({ n,1 });
cloud.push_back({ n,2 });
cloud.push_back({ n-1,1 });
cloud.push_back({ n-1,2 });
for (int i = 0; i < m; i++) {
cin >> d >> s;
d--;
MoveCloud();
PlusWater();
Magic();
MakeCloud();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
answer += map[i][j];
}
}
cout << answer << endl;
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
치즈 C++ (0) | 2022.03.07 |
---|---|
마법사 상어와 토네이도 C++ (0) | 2022.02.15 |
마법사 상어와 블리자드 C++ (0) | 2022.02.03 |
어항 정리 C++ (0) | 2022.01.22 |
마법사 상어와 복제 (0) | 2021.12.03 |