시뮬레이션 문제이다. 주어진 조건에 맞추어 구현하면 됨.
까다로웠던 점
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int N, rst, cx, cy, dir;
int map[500][500];
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
int DX[4][10] = { { -1, 1, -1, 1, -1, 1, -2, 2, 0, 0 },{ -1, 1, -1, 1, -1, 1, -2, 2, 0, 0 },
{ 0, 0, 1, 1, 2, 2, 1, 1, 3, 2}, { 0, 0, -1, -1, -2, -2, -1, -1, -3, -2} };
int DY[4][10] = { { 0, 0, 1, 1, 2, 2, 1, 1, 3, 2}, { 0, 0, -1, -1, -2, -2, -1, -1, -3, -2},
{ -1, 1, -1, 1, -1, 1, -2, 2, 0, 0}, {-1, 1, -1, 1, -1, 1, -2, 2, 0, 0} };
int percent[9] = { 1, 1, 7, 7, 10, 10, 2, 2, 5 };
void input() {
cin >> N;
memset(map, 0, sizeof(map));
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
cin >> map[i][j];
}
}
}
int is_range(int x, int y) {
if (x >= 1 && x <= N && y >= 1 && y <= N) return true;
return false;
}
int change_dir(int Dir) {
if (Dir == 0) return 3;
if (Dir == 1) return 2;
if (Dir == 2) return 0;
if (Dir == 3) return 1;
}
void spread() {
int xx = cx + dx[dir];
int yy = cy + dy[dir];
if (map[xx][yy] == 0) return;
int Sand = map[xx][yy];
int Temp = Sand;
for (int i = 0; i < 9; i++)
{
int nx = cx + DX[dir][i];
int ny = cy + DY[dir][i];
int Plus = (Temp * percent[i]) / 100;
if (!is_range(nx, ny)) rst += Plus;
else map[nx][ny] += Plus;
Sand -= Plus;
}
int nx = cx + DX[dir][9];
int ny = cy + DY[dir][9];
if (!is_range(nx, ny)) rst += Sand;
else map[nx][ny] += Sand;
map[xx][yy] = 0;
}
void solve() {
cx = (N + 1) / 2; cy = (N + 1) / 2; dir = 1; rst = 0;
int move_count = 1;
while (true) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < move_count; j++) {
spread();
cx += dx[dir];
cy += dy[dir];
}
dir = change_dir(dir);
}
move_count++;
if (move_count == N) {
for (int j = 0; j < move_count; j++) {
spread();
cx += dx[dir];
cy += dy[dir];
}
break;
}
}
}
int main() {
input();
solve();
cout << rst << endl;
}