釣り王(17143号)


白駿-釣り王(17143号)
試験場で会ったら1時間以内には解けないだろう
タイムアウトしたので、考えて、探して、サメの移動の論理を修正して、パスしました.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

typedef struct __shark_info {
	int r; // 행
	int c; // 열
	int s; // 속력
	int d; // 이동 방향
	int z; // 크기
}shark_info;

int r, c, m;
int rowDir[4] = { -1, 1, 0, 0 };
int colDir[4] = { 0, 0, 1, -1 };
shark_info sharks[1 << 20];
vector<shark_info> map[128][128];
vector<shark_info> move_map[128][128];

int answer;

void solve(int mrow)
{
	for (int j = 0; j < r; j++) {
		if (!map[j][mrow].empty()) {
			answer += map[j][mrow][0].z;
			map[j][mrow].clear();
			break;
		}
	}

	// 상어 이동
	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			if (map[i][j].empty())
				continue;

			shark_info &shark = map[i][j][0];
			int row = shark.r;
			int col = shark.c;
			int nrow = row, ncol = col;
			if (shark.d == 0) {
				nrow = (r - 1) * 2 - nrow + shark.s;
				nrow = nrow % ((r - 1) * 2);
				shark.d = 1;
				if (nrow > r - 1) {
					nrow = (r - 1) * 2 - nrow;
					shark.d = 0;
				}
			}
			else if (shark.d == 1) {
				nrow = nrow + shark.s;
				nrow = nrow % ((r - 1) * 2);
				if (nrow > r - 1) {
					nrow = (r - 1) * 2 - nrow;
					shark.d = 0;
				}
			}
			else if (shark.d == 2) {
				ncol = ncol + shark.s;
				ncol = ncol % ((c - 1) * 2);
				if (ncol > c - 1) {
					ncol = (c - 1) * 2 - ncol;
					shark.d = 3;
				}
			}
			else if (shark.d == 3) {
				ncol = (c - 1) * 2 - ncol + shark.s;
				ncol = ncol % ((c - 1) * 2);
				shark.d = 2;
				if (ncol > c - 1) {
					ncol = (c - 1) * 2 - ncol;
					shark.d = 3;
				}
			}
			
			shark.r = nrow;
			shark.c = ncol;
			if(move_map[nrow][ncol].empty())
				move_map[nrow][ncol].push_back(shark);
			else {
				if (shark.z > move_map[nrow][ncol][0].z)
					move_map[nrow][ncol][0] = shark;
			}
			map[i][j].clear();
		}
	}

	for (int i = 0; i < r; i++) {
		for (int j = 0; j < c; j++) {
			if (move_map[i][j].empty())
				continue;
			
			shark_info alive = move_map[i][j][0];
			map[i][j].push_back(alive);
			move_map[i][j].clear();
		}
	}
}

int main(void)
{
	cin.tie(0);
	ios_base::sync_with_stdio(0);

	cin >> r >> c >> m;
	for (int i = 0; i < m; i++) {
		cin >> sharks[i].r >> sharks[i].c >> sharks[i].s >> sharks[i].d >> sharks[i].z;
		sharks[i].r--; sharks[i].c--; sharks[i].d--;
		map[sharks[i].r][sharks[i].c].push_back(sharks[i]);
	}

	answer = 0;
	for (int t = 0; t < c; t++) 
		solve(t);

	cout << answer << "\n";

	return 0;
}