Skip to content

Instantly share code, notes, and snippets.

@niklasjang
Created April 26, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklasjang/62123bafe459e58ad0344681a7240f92 to your computer and use it in GitHub Desktop.
Save niklasjang/62123bafe459e58ad0344681a7240f92 to your computer and use it in GitHub Desktop.
[PS][시뮬레이션][이동시키기]/[BOJ][17143][낚시왕]
//12시 49분 시작
#include <iostream>
#include <vector>
using namespace std;
struct shark{
int r,c,s,d,z;
shark(){
r = c = s = d = z = 0;
}
shark(int r, int c, int s, int d, int z)
: r(r), c(c), s(s), d(d), z(z){
//empty
}
};
int n,m,k;
int ans = 0;
vector<int> map[110][110];
vector<shark> fish;
int dr[5] = {0,-1,1,0,0};
int dc[5] = {0,0,0,1,-1};
int nextdir[5] = {0,2,1,4,3};
void shark_remove(int idx){
fish[idx] = fish[k-1];
k--;
}
void shark_hunt(int man){
for(int row=1; row<=n; row++){
if(!map[row][man].empty()){
// cout<<"catch shark size : "<< fish[map[row][man][0]].z <<"\n";
ans += fish[map[row][man][0]].z;
shark_remove(map[row][man][0]);
map[row][man].clear();
break;
}
}
//물고기 정보는 삭제하지 않고
//물고기가 겹치는 정보는 모두 삭제한다.
//hunt 다음이 move이기 때문에 hunt이후에는 move할 때 일반 map 비어있다.
for(int i=0; i<k; i++){
map[fish[i].r][fish[i].c].clear();
}
}
bool in_range(int x, int y){
return 1<= x && x<=n && 1<=y && y<=m;
}
void shark_move(void){
int i=0, step=0;
for(i=0; i < k; i++){
int r = fish[i].r;
int c = fish[i].c;
int s = fish[i].s;
int d = fish[i].d;
int z = fish[i].z;
while(s >= 0){
// cout<< r<<","<<c<<","<<s<<","<<d<<","<<z<<"\n";
int nr = r + s * dr[d];
int nc = c + s * dc[d];
if(in_range(nr,nc)){
fish[i] = {nr,nc,fish[i].s,d,z};
if(map[nr][nc].empty()){
map[nr][nc].push_back(i);
}else{
if(fish[map[nr][nc][0]].z < fish[i].z){
fish[map[nr][nc][0]] = fish[i];
shark_remove(i);
i--;
}else{
shark_remove(i);
i--;
}
}
break;
}
//최대로 직진하고 방향도 바꿔야 함
if(d == 1){
s = s - (r - 1);
r = 1;
}else if(d == 2){
s = s - (n - r);
r = n;
}else if(d == 3){
s = s - (m - c);
c = m;
}else if(d == 4){
s = s - (c - 1);
c = 1;
}
d = nextdir[d];
}
}
}
int main (void){
cin.tie(NULL);
ios::sync_with_stdio("false");
// freopen("data/in5.txt", "r", stdin);
cin>> n >> m >> k;
if(k == 0){
cout<< 0<<"\n";
return 0;
}
for(int i=0; i<k; i++){
int r,c,s,d,z;
cin>> r>> c>> s >> d>> z;
if (d == 1 || d == 2) s %= 2 * n - 2;
else s %= 2 * m - 2;
fish.push_back({r,c,s,d,z});
map[r][c].push_back(i);
}
for(int man = 1; man<=m; man++){
shark_hunt(man);
shark_move();
}
cout << ans <<"\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment