https://www.acmicpc.net/problem/17143
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dy[5] = {0, -1, 1, 0, 0};
int dx[5] = {0, 0, 0, 1, -1};
struct shark
{
int r;
int c;
int s;
int d;
int z;
};
int R, C, M;
vector<vector<shark>> arr(201, vector<shark>(201));
vector<vector<shark>> t_arr(201, vector<shark>(201));
int fisher = 0;
int answer;
void fishing()
{
for (int i = 1; i <= R; i++)
{
if (arr[i][fisher].c != 0)
{
answer += arr[i][fisher].z;
shark temp = {0, 0, 0, 0, 0};
arr[i][fisher] = temp;
break;
}
}
}
void move_shark(int y, int x)
{
int r = arr[y][x].r;
int c = arr[y][x].c;
int s = arr[y][x].s;
int d = arr[y][x].d;
int z = arr[y][x].z;
if(d == 1){
int temp = s- (r-1);
if(temp <= 0){
r += dy[d] * s;
c += dx[d] * s;
}
else{
r = 1;
int temp2 = temp % (R-1);
int temp3= temp / (R-1);
if(temp3 % 2 == 0){
d = 2;
}
else{
r = R;
}
r += dy[d] * temp2;
c += dx[d] * temp2;
}
}
else if(d == 2){
int temp = s-(R-r);
if(temp <= 0){
r += dy[d] * s;
c += dx[d] * s;
}
else{
r = R;
int temp2 = temp % (R-1);
int temp3 = temp / (R-1);
if(temp3 % 2 == 0){
d = 1;
}
else{
r = 1;
}
r += dy[d] * temp2;
c += dx[d] * temp2;
}
}
else if(d == 3){
int temp = s-(C-c);
if(temp <= 0){
r += dy[d] * s;
c += dx[d] * s;
}
else{
c = C;
int temp2 = temp % (C-1);
int temp3 = temp / (C-1);
if(temp3 % 2 == 0){
d = 4;
}
else{
c = 1;
}
r += dy[d] * temp2;
c += dx[d] * temp2;
}
}
else if(d == 4){
int temp = s- (c-1);
if(temp <= 0){
r += dy[d] * s;
c += dx[d] * s;
}
else{
c = 1;
int temp2 = temp % (C-1);
int temp3= temp / (C-1);
if(temp3 % 2 == 0){
d = 3;
}
else {
c = C;
}
r += dy[d] * temp2;
c += dx[d] * temp2;
}
}
if (t_arr[r][c].c != 0)
{
if (t_arr[r][c].z > z)
{
}
else
{
t_arr[r][c] = {r, c, s, d, z};
}
}
else
{
t_arr[r][c] = {r, c, s, d, z};
}
}
void Solution()
{
while (fisher <= C)
{
fisher++;
for (int i = 1; i <= R; i++)
{
for (int j = 1; j <= C; j++)
{
t_arr[i][j] = {0, 0, 0, 0, 0};
}
}
fishing();
for (int i = 1; i <= R; i++)
{
for (int j = 1; j <= C; j++)
{
if (arr[i][j].c != 0)
{
move_shark(i, j);
}
}
}
for (int i = 1; i <= R; i++)
{
for (int j = 1; j <= C; j++)
{
arr[i][j] = t_arr[i][j];
}
}
}
cout << answer << '\n';
}
void Input()
{
cin >> R >> C >> M;
for (int i = 0; i < M; i++)
{
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
shark temp = {a, b, c, d, e};
arr[a][b] = temp;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
Input();
Solution();
}