본문 바로가기
백준 알고리즘/시뮬레이션

백준 낚시왕 C++

by paysmile 2019. 10. 17.

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>

using namespace std;
const int MAX = 101;
vector<pair<int,pair<int,int>>> shark[MAX][MAX];
int r, c, m;
int hunter = 0;

struct Move {
	int x, y;
};
Move mv[4] = { {-1,0} , {1,0}, {0,1}, {0,-1} };

int main(void) {
	int answer = 0;
	cin >> r >> c >> m;

	for (int i = 0; i < m; i++) {
		int r, c, s, d, z;
		cin >> r >> c >> s >> d >> z;
		shark[r][c].push_back(make_pair(s, make_pair(d-1, z)));
	}
	while (hunter < c) {
		hunter++;
		//catch shark
		for (int i = 1; i <= r; i++) {
			if (shark[i][hunter].size() > 0) {
				answer += shark[i][hunter][0].second.second;
				shark[i][hunter].clear();
				break;
			}
		}
		queue <pair<pair<int, int>, pair<int, pair<int, int>>>> q;
		for (int i = 1; i <= r; i++) {
			for (int j = 1; j <= c; j++) {
				if (shark[i][j].size() > 0) {
					q.push(make_pair(make_pair(i, j), make_pair(shark[i][j][0].first, make_pair(shark[i][j][0].second.first, shark[i][j][0].second.second))));
					shark[i][j].clear();
				}
			}
		}
		//move shark
		while (!q.empty()) {
			int i = q.front().first.first;
			int j = q.front().first.second;
			int s = q.front().second.first;
			int d = q.front().second.second.first;
			int z = q.front().second.second.second;
			q.pop();
			int movei = i;
			int movej = j;
			for (int k = 0; k < s; k++) {
				if (d == 0 || d == 1) {
					int temp = movei + mv[d].x;
					if (!(temp >= 1 && temp <= r))
						d = 1 - d;
					movei += mv[d].x;
				}
				else {
					int temp = movej + mv[d].y;
					if (!(temp >= 1 && temp <= c))
						d = 5 - d;
					movej += mv[d].y;
				}
			}
			if (shark[movei][movej].size() > 0) {
				if (shark[movei][movej][0].second.second < z) {
					shark[movei][movej][0] = make_pair(s, make_pair(d, z));
				}
			}
			else {
				shark[movei][movej].push_back(make_pair(s, make_pair(d, z)));
			}
		}
	}
	cout << answer << endl;
	return 0;
}

'백준 알고리즘 > 시뮬레이션' 카테고리의 다른 글

어른 상어 C++  (0) 2021.04.22
백준 이차원 배열과 연산 C++  (0) 2019.10.17
백준 나무 재테크 C++  (0) 2019.10.16
백준 큐빙 C++  (0) 2019.10.15
백준 1152번 C++  (0) 2019.08.30