문제 URL : www.acmicpc.net/problem/16235
16235번: 나무 재테크
부동산 투자로 억대의 돈을 번 상도는 최근 N×N 크기의 땅을 구매했다. 상도는 손쉬운 땅 관리를 위해 땅을 1×1 크기의 칸으로 나누어 놓았다. 각각의 칸은 (r, c)로 나타내며, r은 가장 위에서부터
www.acmicpc.net
문제 접근법 :
따로 드릴얘기가 없네요
봄 여름 가을 겨울 이 하란대로 그대로 하면됩니다.
코드만 봐도 주석이있기 때문에 직관적으로 이해하기 쉬울겁니다.
소스코드 :
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
//By 콩순이냉장고
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,k;
int map[10][10];
int A[10][10];
vector<int> tree[10][10];
int dy[8] = { -1,-1,-1,0,0,1,1,1 };
int dx[8] = { -1,0,1,-1,1,-1,0,1 };
struct Tree {
int y, x, age;
Tree(int y, int x, int age) :y(y), x(x), age(age) {}
};
vector<Tree> deadTree,growTree;
void input() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> A[i][j];
map[i][j] = 5; //가장 처음에 양분은 모든 칸에 5만큼 들어있다
}
for (int i = 0; i < m; i++) {//m개의 나무를 구매해 땅에 심었다.
int y, x, age;
cin >> y >> x >> age;
tree[y-1][x-1].push_back(age);
}
}
bool isrange(int y, int x) {
return 0 <= y && y < n && 0 <= x && x < n;
}
void spring() {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) {
sort(tree[i][j].begin(), tree[i][j].end());//어린나무부터
vector<int> v;
for (int k = 0; k < tree[i][j].size(); k++) {//하나의 칸에 여러개의 나무
if (map[i][j] - tree[i][j][k] >= 0) {
map[i][j] -= tree[i][j][k];
v.push_back(tree[i][j][k]+1);
if ((tree[i][j][k] + 1) % 5 == 0)
growTree.push_back({ i,j,tree[i][j][k] + 1 });
}
else {
deadTree.push_back({i,j,tree[i][j][k]});
}
}
tree[i][j] = v;
}
}
}
void summer() {
for (int i = 0; i < deadTree.size(); i++)//봄에 죽은 나무가 양분으로 변하게된다
{
int y = deadTree[i].y;
int x = deadTree[i].x;
int age = deadTree[i].age;
map[y][x] += age / 2;
}
deadTree.clear();
}
void autumn(){
for (int i = 0; i < growTree.size(); i++) {
int y = growTree[i].y;
int x = growTree[i].x;
int age = growTree[i].age;
for (int j = 0; j < 8; j++) {
int ny = y + dy[j];
int nx = x + dx[j];
if (isrange(ny, nx))
tree[ny][nx].push_back(1);
}
}
growTree.clear();
}
void winter() {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) {
map[i][j] += A[i][j];
}
}
}
void solve()
{
while (k--) {
spring();
summer();
autumn();
winter();
}
int res = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++) {
res += tree[i][j].size();
}
}
cout << res << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
input();
solve();
}
|
cs |
궁금한점 혹은 모르는점 혹은 어떤지적이든 댓글은 환영입니다.
'백준' 카테고리의 다른 글
백준 3745 오름세 (0) | 2021.02.15 |
---|---|
백준 9370 미확인 도착지 (0) | 2021.02.15 |
백준 16920 확장 게임 (0) | 2021.02.15 |
백준 1806 부분합 (0) | 2021.02.15 |
백준 20057 마법사 상어와 토네이도 (0) | 2021.02.10 |