코드
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
struct st {
int fm, fs, fd;
};
const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, M, k, r, c, m, s, d, res;
vector<st> a[52][52];
vector<st> b[52][52];
void go() {
// 이동
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (st si : a[i][j]) {
int nx = (i + dx[si.fd] * si.fs) % n;
int ny = (j + dy[si.fd] * si.fs) % n;
// 음수 인덱스 처리
if (nx < 0) nx += n;
if (ny < 0) ny += n;
b[nx][ny].push_back({si.fm, si.fs, si.fd});
}
}
}
// b 초기화 및 a로 복사
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = b[i][j];
b[i][j].clear();
}
}
// 합치기 작업
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j].size() > 1) {
int nm = 0, ns = 0;
bool isodd = true, iseven = true;
for (int _size = 0; _size < a[i][j].size(); _size++) {
nm += a[i][j][_size].fm;
ns += a[i][j][_size].fs;
if (a[i][j][_size].fd % 2 == 1) iseven = false;
if (a[i][j][_size].fd % 2 == 0) isodd = false;
}
nm /= 5;
ns /= a[i][j].size();
a[i][j].clear();
if (nm == 0) continue;
if (iseven || isodd) {
a[i][j].push_back({nm, ns, 0});
a[i][j].push_back({nm, ns, 2});
a[i][j].push_back({nm, ns, 4});
a[i][j].push_back({nm, ns, 6});
} else {
a[i][j].push_back({nm, ns, 1});
a[i][j].push_back({nm, ns, 3});
a[i][j].push_back({nm, ns, 5});
a[i][j].push_back({nm, ns, 7});
}
}
}
}
}
int main() {
cin >> n >> M >> k;
for (int i = 0; i < M; i++) {
cin >> r >> c >> m >> s >> d;
a[r - 1][c - 1].push_back({m, s, d});
}
while (k--) {
go();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (st si : a[i][j]) {
res += si.fm;
}
}
}
cout << res << '\n';
return 0;
}
풀이