본문 바로가기
PS(Problem Solving)/백준_BOJ

[백준] 21610번 - 마법사 상어와 비바라기 (C++) 문제 및 풀이

by 초코칩프라푸치노 2022. 2. 17.

문제) 백준 - 구현 - 마법사 상어와 비바라기

https://www.acmicpc.net/problem/21610

 

21610번: 마법사 상어와 비바라기

마법사 상어는 파이어볼, 토네이도, 파이어스톰, 물복사버그 마법을 할 수 있다. 오늘 새로 배운 마법은 비바라기이다. 비바라기를 시전하면 하늘에 비구름을 만들 수 있다. 오늘은 비바라기

www.acmicpc.net

 

문제에 제시된 설명대로 구현하면 해결할 수 있는 문제였습니다. 

 

int A[i][j]: i 행 j 열에 담긴 물의 양

int cloud[i][j]: i 행 j 열의 구름 여부, 1일 경우 이전 구름/2일 경우 생성된 구름

chgRange(x): x 범위 예외 처리

moveCloud(d, s): 구름을 d의 방향으로 s만큼 이동

rain(): 구름이 존재하는 자리에 바구니에 저장된 물의 양 +1

waterCopyBug(): 구름이 존재한 자리에 대각선을 확인하며 물의 양 증가

makeCloud(): 바구니에 들은 물의 양이 2 이상일 경우 구름 생성 후 기존 구름 제거

result(): M번의 이동 후, 바구니에 들은 모든 물의 합

 

C++ 소스코드)

int chgRange(int x){
if(x == 0) return N;
else if(x == N + 1) return 1;
else return x;
}
void moveCloud(int d, int s){
vector<p> vc;
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j){
if(cloud[i][j] == 1){
int nx = i;
int ny = j;
for(int k = 0; k < s;++k){
nx += Dir[d].first;
ny += Dir[d].second;
nx = chgRange(nx);
ny = chgRange(ny);
}
vc.push_back({nx, ny});
cloud[i][j] = 0;
}
}
for(p a: vc)
cloud[a.first][a.second] = 1;
}
void rain(){
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
if(cloud[i][j] == 1)
A[i][j]++;
}
void waterCopyBug(){
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j){
if(cloud[i][j] == 1){
int temp = 0;
for(int d = 1; d <= 4; ++d){
int nx = i + Dir[d * 2].first;
int ny = j + Dir[d * 2].second;
if(valid(nx, ny) && A[nx][ny] > 0)
temp++;
}
A[i][j] += temp;
}
}
}
void makeCloud(){
for(int i = 1; i <=N; ++i)
for(int j = 1; j <= N; ++j){
if(A[i][j] >= 2 && cloud[i][j] != 1){
A[i][j] -= 2;
cloud[i][j] = 2;
}
}
// 기존 구름 제거
for(int i = 1; i <=N; ++i)
for(int j = 1; j <= N; ++j)
if(cloud[i][j] == 1)
cloud[i][j] = 0;
else if(cloud[i][j] == 2)
cloud[i][j] = 1;
}
int result(){
int res = 0;
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j)
res += A[i][j];
return res;
}
view raw 21610.cpp hosted with ❤ by GitHub

Full Code)

https://github.com/Chocochip101/BOJ_Solution/blob/main/Solution/21610_%EB%A7%88%EB%B2%95%EC%82%AC%EC%83%81%EC%96%B4%EC%99%80%EB%B9%84%EB%B0%94%EB%9D%BC%EA%B8%B0.cpp

 

GitHub - Chocochip101/BOJ_Solution: BOJ Solutions

BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.

github.com

 

반응형

댓글