https://www.acmicpc.net/problem/21610
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAX = 52;
struct MOVE { int x, y; };
MOVE mv[8] = { { 0,-1 },{ -1,-1 },{ -1,0 },{ -1,1 },{ 0,1 },{ 1,1 },{ 1,0 },{ 1,-1 } };
int n, m;
int map[MAX][MAX];
vector <pair<int, int>> cloud, temp;
//1,2번
void MoveCloud(int d, int s) {
for (int i = 0; i < cloud.size(); i++) {
int movei = cloud[i].first + mv[d - 1].x * s;
int movej = cloud[i].second + mv[d - 1].y * s;
while (movei > n) movei -= n;
while (movej > n) movej -= n;
while (movei <= 0) movei += n;
while (movej <= 0) movej += n;
map[movei][movej] += 1;
cloud[i] = make_pair(movei, movej);
}
}
//4번
void IncCloud() {
//{-1,-1}->1, {-1,1}->3, {1,-1}->7, {1,1}->5
for (int i = 0; i < cloud.size(); i++) {
int count = 0;
for (int k = 1; k <= 7; k += 2) {
int movei = cloud[i].first + mv[k].x;
int movej = cloud[i].second + mv[k].y;
if (movei > 0 && movei <= n && movej > 0 && movej <= n) {
if (map[movei][movej]> 0) count += 1;
}
}
map[cloud[i].first][cloud[i].second] += count;
}
}
//5번
void MakeCloud() {
temp.clear();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (map[i][j] >= 2) {
bool flag = false;
for (int k = 0; k < cloud.size(); k++) {
if (cloud[k] == make_pair(i, j)) {
flag = true;
break;
}
}
if (flag == false) {
map[i][j] -= 2;
temp.push_back(make_pair(i, j));
}
}
}
}
cloud = temp;
}
void printmap() {
cout << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
}
int main(void) {
int answer = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> map[i][j];
}
}
cloud.push_back(make_pair(n, 1));
cloud.push_back(make_pair(n, 2));
cloud.push_back(make_pair(n - 1, 1));
cloud.push_back(make_pair(n - 1, 2));
for (int i = 0; i < m; i++) {
int d, s;
cin >> d >> s;
MoveCloud(d, s);
IncCloud();
MakeCloud();
//printmap();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
answer += map[i][j];
}
}
cout << answer << endl;
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
백준 모노미노도미노 2 C++ (0) | 2021.07.26 |
---|---|
백준 주사위 윷놀이 C++ (0) | 2021.07.21 |
상어 중학교 (0) | 2021.06.28 |
새로운 게임 2 (0) | 2021.04.20 |
주사위 윷놀이 C++ (0) | 2021.04.20 |