반응형
https://www.acmicpc.net/problem/21610
21610번: 마법사 상어와 비바라기
마법사 상어는 파이어볼, 토네이도, 파이어스톰, 물복사버그 마법을 할 수 있다. 오늘 새로 배운 마법은 비바라기이다. 비바라기를 시전하면 하늘에 비구름을 만들 수 있다. 오늘은 비바라기
www.acmicpc.net
[ 문제풀이 ]
1. 구름의 위치를 vector에 저장합니다.
2. 방향 배열을 이용하여 구름을 이동시킵니다.
3. 비를 내리고, 바구니 배열을 갱신합니다.
4. 물복사버그를 적용합니다.
5. 원래 구름이 있었던 자리를 temp 배열에 저장한 후, 나머지 자리에 구름을 생성해줍니다.
6. 위 과정을 반복해줍니다.
[ 소스코드 ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include<iostream> #include<vector> #include<cstring> using namespace std; int N, M; int arr[51][51]; int temp[51][51]; int dy[9] = { 0,0,-1,-1,-1,0,1,1,1 }; int dx[9] = { 0,-1,-1,0,1,1,1,0,-1 }; vector<pair<int, int>> cloud; void makeCloud() //구름 만들기 { cloud.clear(); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (temp[i][j] != 1 && arr[i][j] >=2) { cloud.push_back({ i,j }); arr[i][j] -= 2; } } } memset(temp, 0, sizeof(temp)); } void bug() //물복사버그 { for (auto next : cloud) { int cnt = 0; for (int i = 2; i <= 8; i += 2) { int yy = next.first + dy[i]; int xx = next.second + dx[i]; if (yy > 0 && yy <= N && xx > 0 && xx <= N) { if (arr[yy][xx] > 0) cnt++; } } arr[next.first][next.second] += cnt; } } void rain() //비 내리기 { for (auto next : cloud) { int y = next.first; int x = next.second; temp[y][x] = 1; arr[y][x]++; } } void move(int d, int s) //구름 이동 { for (int i=0;i<cloud.size();i++) { int yy = (cloud[i].first + dy[d] * s)%N; int xx = (cloud[i].second + dx[d] * s)%N; if (yy < 1) yy += N; if (xx < 1)xx += N; cloud[i].first = yy; cloud[i].second = xx; } } int main() { scanf("%d %d", &N, &M); cloud.push_back({ N,1 }); cloud.push_back({ N,2 }); cloud.push_back({ N-1,1 }); cloud.push_back({ N-1,2 }); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { scanf("%d", &arr[i][j]); } } for (int i = 0; i < M; i++) { int d, s; scanf("%d %d", &d, &s); move(d, s); rain(); bug(); makeCloud(); } int ans = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { ans += arr[i][j]; } } printf("%d", ans); } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 1012번 - 유기농 배추 (C++) (0) | 2022.09.19 |
---|---|
[ 백준 ] 11723번 - 집합 (C++) (0) | 2022.09.18 |
[ 백준 ] 17779번 - 게리맨더링 2 (C++) (0) | 2022.09.16 |
[ 백준 ] 5373번 - 큐빙 (C++) (0) | 2022.09.15 |
[ 백준 ] 17140번 - 이차원 배열과 연산 (C++) (0) | 2022.09.14 |