Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- BFS
- 브루트포스
- 자료구조
- XR Interaction Toolkit
- 알고리즘
- 다익스트라
- 다이나믹 프로그래밍
- Team Fortress 2
- 스택
- 백준
- 수학
- 유니티
- 그래프
- 투 포인터
- VR
- Unreal Engine 5
- 그리디 알고리즘
- DFS
- 문자열
- 구현
- 우선순위 큐
- 재귀
- c++
- ue5
- 백트래킹
- 정렬
- 누적 합
- 트리
- 유니온 파인드
- 시뮬레이션
Archives
- Today
- Total
1일1알
백준 17144번 미세먼지 안녕! C++ 본문
문제에 써있는대로 구현하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int r, c, t;
vector<pair<int, int>> airCleaner(2);
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
void SpreadDust(vector<vector<int>>& board) {
vector<vector<int>> tmp(r, vector<int>(c, 0));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board[i][j] == 0) continue;
int spreadAmount = board[i][j] / 5;
int cnt = 0;
for (int k = 0; k < 4; k++) {
int nextRow = i + dRow[k];
int nextCol = j + dCol[k];
if (nextRow < 0 || nextRow >= r) continue;
if (nextCol < 0 || nextCol >= c) continue;
if (board[nextRow][nextCol] == -1) continue;
tmp[nextRow][nextCol] += spreadAmount;
cnt++;
}
tmp[i][j] -= spreadAmount * cnt;
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
board[i][j] += tmp[i][j];
}
}
}
void WorkAirCleaner(vector<vector<int>>& board) {
for (int i = 0; i < 2; i++) {
int dir;
int maxRow = r - 1;
int minRow = 0;
if (i == 0) {
dir = 0;
maxRow = airCleaner[i].first;
}
else {
dir = 2;
minRow = airCleaner[i].first;
}
int currRow = airCleaner[i].first + dRow[dir];
int currCol = airCleaner[i].second + dCol[dir];
while (currRow != airCleaner[i].first || currCol != airCleaner[i].second) {
int revDir = (dir + 2) % 4;
int lastRow = currRow + dRow[revDir];
int lastCol = currCol + dCol[revDir];
if (board[lastRow][lastCol] == 0) {
board[lastRow][lastCol] = board[currRow][currCol];
}
board[currRow][currCol] = 0;
int nextRow = currRow + dRow[dir];
int nextCol = currCol + dCol[dir];
if (nextRow < minRow || nextRow > maxRow || nextCol < 0 || nextCol >= c) {
if (i == 0) dir = (dir + 1) % 4;
else dir = (dir + 3) % 4;
nextRow = currRow + dRow[dir];
nextCol = currCol + dCol[dir];
}
currRow = nextRow;
currCol = nextCol;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> r >> c >> t;
vector<vector<int>> board(r, vector<int>(c));
int num = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> board[i][j];
if (board[i][j] == -1) {
airCleaner[num] = { i,j };
num++;
}
}
}
for (int i = 0; i < t; i++) {
SpreadDust(board);
WorkAirCleaner(board);
}
int ans = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board[i][j] == -1) continue;
ans += board[i][j];
}
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1189번 컴백홈 C++ (0) | 2022.05.28 |
---|---|
백준 2193번 이친수 C++ (0) | 2022.05.26 |
백준 1655번 가운데를 말해요 C++ (0) | 2022.05.24 |
백준 16235번 나무 재테크 C++ (0) | 2022.05.23 |
백준 12100번 2048 (Easy) C++ (0) | 2022.05.21 |