Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created August 9, 2019 15:53
Show Gist options
  • Save surinoel/d1a9e73a24971130035e53e1cbf6cc14 to your computer and use it in GitHub Desktop.
Save surinoel/d1a9e73a24971130035e53e1cbf6cc14 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef struct __shark {
int dir, vc, size;
__shark() : dir(0), vc(0), size(0) {}
__shark(int dir, int vc, int size) :
dir(dir), vc(vc), size(size) {}
} SHARK;
typedef struct __sharkinfo {
int x, y, dir, vc, size;
__sharkinfo() : x(0), y(0), dir(0), vc(0), size(0) {}
__sharkinfo(int x, int y, int dir, int vc, int size) :
x(x), y(y), dir(dir), vc(vc), size(size) {}
} SHARKINFO;
bool cmp(const SHARK &u, const SHARK &v) {
return u.size > v.size;
}
int dx[5] = { 0, -1, 1, 0, 0 };
int dy[5] = { 0, 0, 0, 1, -1 };
bool check[100][100];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector<vector<vector<SHARK>>> mat(n, vector<vector<SHARK>>(m, vector<SHARK>()));
vector<SHARKINFO> sharkinfo;
for (int i = 0; i < k; i++) {
int x, y, s, d, z;
cin >> x >> y >> s >> d >> z;
x -= 1, y -= 1;
mat[x][y].push_back(SHARK(d, s, z));
sharkinfo.push_back(SHARKINFO(x, y, d, s, z));
}
int ans = 0;
for (int col = 0; col < m; col++) {
int cx = -1, cy = -1;
for (int i = 0; i < n; i++) {
if (mat[i][col].size() > 0) {
cx = i, cy = col;
ans += mat[i][col][0].size;
mat[i][col].erase(mat[i][col].begin(), mat[i][col].end());
break;
}
}
vector<vector<vector<SHARK>>> tmpmat(n, vector<vector<SHARK>>(m, vector<SHARK>()));
vector<SHARKINFO> tmp_sharkinfo;
for (int i = 0; i < sharkinfo.size(); i++) {
int x, y, s, d, z;
x = sharkinfo[i].x;
y = sharkinfo[i].y;
s = sharkinfo[i].vc;
d = sharkinfo[i].dir;
z = sharkinfo[i].size;
if (x == cx && y == cy) continue;
int tx = x, ty = y;
for (int j = 0; j < s; j++) {
tx += dx[d];
ty += dy[d];
if (tx < 0 || ty < 0 || tx > n - 1 || ty > m - 1) {
if (d == 1) d = 2;
else if (d == 2) d = 1;
else if (d == 3) d = 4;
else if (d == 4) d = 3;
j -= 1;
tx += dx[d];
ty += dy[d];
continue;
}
}
tmp_sharkinfo.push_back(SHARKINFO(tx, ty, d, s, z));
tmpmat[tx][ty].push_back(SHARK(d, s, z));
}
memset(check, 0, sizeof(check));
sharkinfo = vector<SHARKINFO>();
for (int i = 0; i < tmp_sharkinfo.size(); i++) {
int x, y;
x = tmp_sharkinfo[i].x;
y = tmp_sharkinfo[i].y;
sort(tmpmat[x][y].begin(), tmpmat[x][y].end(), cmp);
if (tmpmat[x][y].size() > 1) {
tmpmat[x][y].erase(tmpmat[x][y].begin() + 1, tmpmat[x][y].end());
}
if (!check[x][y]) {
check[x][y] = true;
sharkinfo.push_back(SHARKINFO(x, y, tmpmat[x][y].begin()->dir, tmpmat[x][y].begin()->vc, tmpmat[x][y].begin()->size));
}
}
mat = tmpmat;
}
cout << ans << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment