https://www.acmicpc.net/problem/20056
20056번: 마법사 상어와 파이어볼
첫째 줄에 N, M, K가 주어진다. 둘째 줄부터 M개의 줄에 파이어볼의 정보가 한 줄에 하나씩 주어진다. 파이어볼의 정보는 다섯 정수 ri, ci, mi, si, di로 이루어져 있다. 서로 다른 두 파이어볼의 위치
www.acmicpc.net
[난이도] Gold5
[유형] 시뮬레이션
[풀이]
문제의 조건대로 정확하게 시뮬레이션 해준다.
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int N,M,K,ans;
int dy[8] = {-1,-1,0,1,1,1,0,-1};
int dx[8] = {0,1,1,1,0,-1,-1,-1};
struct P{
int m,s,d;
};
vector<P> map[51][51],tmp[51][51];
void clear(vector<P> a[][51]){
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++) a[i][j].clear();
}
void move(int i,int j){
for(auto p : map[i][j]){
int ny=i,nx=j;
for(int k=0;k<p.s;k++){
ny+=dy[p.d];
nx+=dx[p.d];
if(ny>N) ny=1;
else if(ny<1) ny=N;
if(nx>N) nx=1;
else if(nx<1) nx=N;
}
tmp[ny][nx].push_back(p);
}
}
void merge(int y,int x){
if(tmp[y][x].size()<2) {
map[y][x]=tmp[y][x];
return;
}
int sm=0,ss=0,sd=0;
for(auto p : tmp[y][x]){
sm+=p.m;
ss+=p.s;
sd+=p.d%2;
}
int sz = tmp[y][x].size();
int tm = sm/5;
if(tm==0) return;
int ts = ss/sz;
int td;
if(sd == sz || sd == 0) td = 0;
else td = 1;
for(;td<8;td+=2) map[y][x].push_back({tm,ts,td});
}
void cmd(){
clear(tmp);
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
move(i,j);
}
}
clear(map);
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
merge(i,j);
}
}
}
int main(){
scanf("%d%d%d",&N,&M,&K);
while(M--){
int r,c,m,s,d;
scanf("%d%d%d%d%d",&r,&c,&m,&s,&d);
map[r][c].push_back({m,s,d});
}
while(K--) cmd();
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++){
for(auto p : map[i][j]) ans+=p.m;
}
printf("%d",ans);
}
https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Gold5/20056.cpp
'Problem-Solving > BOJ' 카테고리의 다른 글
[BOJ/백준][Gold5] 17141 : 연구소 2 (C++) (0) | 2021.06.24 |
---|---|
[BOJ/백준][Gold5] 9084 : 동전 (C++) (0) | 2021.06.24 |
[BOJ/백준][Gold5] 1188 : 음식 평론가 (C++) (0) | 2021.06.24 |
[BOJ/백준][Gold5] 14719 : 빗물 (C++) (0) | 2021.06.24 |
[BOJ/백준][Gold5] 10216 : Count Circle Groups (C++) (0) | 2021.06.24 |