https://www.acmicpc.net/problem/17143
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 101;
int r, c, m;
struct sk { int s, d, z, exist; };
vector <sk> shark[MAX][MAX];
int answer = 0;
int loc = 1;
struct MOVE { int x, y; };
MOVE mv[5] = { { 0,0 },{ -1,0 },{ 1,0 },{ 0,1 },{ 0,-1 } };
//dir 1:위, 2:아래, 3:오른쪽, 4:왼쪽
vector <pair<int, int>> shark_loc;
bool cmp2(pair<int, int> a, pair<int, int> b) {
return a.first < b.first;
}
void EatShark() {
int sz = shark_loc.size();
for (int i = 1; i <= r; i++) {
int location = find(shark_loc.begin(), shark_loc.end(), make_pair(i, loc))- shark_loc.begin();
if (location != sz) {
answer += shark[shark_loc[location].first][shark_loc[location].second][0].z;
shark[shark_loc[location].first][shark_loc[location].second].clear();
shark_loc.erase(shark_loc.begin() + location);
return;
}
}
}
void MoveShark() {
vector<pair<int, int>> temp_shark;
for (int id = 0; id < shark_loc.size(); id++) {
int i = shark_loc[id].first;
int j = shark_loc[id].second;
int fast = shark[i][j][0].s;
int dir = shark[i][j][0].d;
int movei = i;
int movej = j;
if (dir == 1 || dir == 2) {
if(fast >= ((r-1)*2))
fast = fast % ((r - 1) * 2);
}
else if (dir == 3 || dir == 4) {
if(fast >= ((c-1)*2))
fast = fast % ((c - 1) * 2);
}
for (int k = 0; k < fast; k++) {
if (dir == 1 && movei == 1) dir = 2;
else if (dir == 2 && movei == r) dir = 1;
else if (dir == 3 && movej == c) dir = 4;
else if (dir == 4 && movej == 1) dir = 3;
movei = movei + mv[dir].x;
movej = movej + mv[dir].y;
}
sk temp = shark[i][j][0];
temp.d = dir;
temp.exist = loc + 1;
//빈자리
if (shark[movei][movej].size() == 0) {
shark[movei][movej].push_back(temp);
temp_shark.push_back(make_pair(movei, movej));
}
//이미 하나 들어옴
else if (shark[movei][movej].size() == 1 && shark[movei][movej][0].exist == loc + 1) {
if (shark[movei][movej][0].z < shark[i][j][0].z) {
shark[movei][movej][0] = temp;
}
}
//전에 있던애만 있음
else if (shark[movei][movej].size() == 1 && shark[movei][movej][0].exist == loc) {
shark[movei][movej].push_back(temp);
temp_shark.push_back(make_pair(movei, movej));
}
//둘다 있음
else if (shark[movei][movej].size() == 2) {
if (shark[movei][movej][1].z < shark[i][j][0].z) {
shark[movei][movej][1] = temp;
}
}
shark[i][j].erase(shark[i][j].begin());
}
shark_loc = temp_shark;
}
int main(void) {
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({ S,D,Z,loc });
shark_loc.push_back(make_pair(R, C));
}
while (loc <= c && shark_loc.size() >0) {
EatShark();
MoveShark();
loc += 1;
}
cout << answer << endl;
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
청소년 상어 C++ (0) | 2021.04.17 |
---|---|
마법사 상어와 파이어볼 C++ (0) | 2021.04.13 |
마법사 상어와 토네이도 C++ (0) | 2021.04.06 |
새로운 게임 2 C++ (0) | 2021.03.19 |
주사위 윷놀이 C++ (0) | 2021.03.10 |