16235 Samsung sw test
코드
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <algorithm>
#include <vector>
#define MAX_WIDTH 10
using namespace std;
typedef struct Tree {
int age;
bool isAlive;
bool operator < (Tree t) {
return age < t.age;
}
} Tree;
typedef struct Cell {
int ntr;
vector<Tree> tree;
} Cell;
Cell map[MAX_WIDTH][MAX_WIDTH];
int nutrition[MAX_WIDTH][MAX_WIDTH];
int width, maxYear;
void growTrees();
int checkAliveTree();
void spring();
void summer();
void fall();
void winter();
int main() {
int tempM;
cin >> width >> tempM >> maxYear;
for (int i = 0; i < width; i++)
for (int j = 0; j < width; j++) {
cin >> nutrition[i][j];
map[i][j].ntr = 5;
}
for (int i = 0; i < tempM; i++) {
int row, col, age;
cin >> row >> col >> age;
map[row-1][col-1].tree.push_back({ age, true });
}
growTrees();
cout << checkAliveTree();
}
void growTrees() {
for (int i = 0; i < maxYear; i++) {
spring();
summer();
fall();
winter();
}
}
void spring() {
//나이순으로 정렬
for (int i = 0; i < width; i++)
for (int j = 0; j < width; j++) {
sort(map[i][j].tree.begin(), map[i][j].tree.end());
}
//양분을 주고, 죽은 건 false 표시하기
for (int i = 0; i < width; i++)
for (int j = 0; j < width; j++)
for (int k = 0; k < map[i][j].tree.size(); k++) {
if (map[i][j].ntr >= map[i][j].tree[k].age) {
map[i][j].ntr -= map[i][j].tree[k].age;
map[i][j].tree[k].age++;
}
else
map[i][j].tree[k].isAlive = false;
}
}
void summer() {
for (int i = 0; i < width; i++)
for (int j = 0; j < width; j++) {
int count = 0;
for (int k = 0; k < map[i][j].tree.size(); k++) {
if (!map[i][j].tree[k].isAlive) {
map[i][j].ntr += (map[i][j].tree[k].age / 2);
count++;
}
}
for (int k = 0; k < count; k++) {
map[i][j].tree.pop_back();
}
}
}
void fall() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
for (int k = 0; k < map[i][j].tree.size(); k++) {
if (map[i][j].tree[k].age % 5 == 0) {
if (i > 0 && j > 0)
map[i - 1][j - 1].tree.push_back({ 1, true });
if (i > 0)
map[i - 1][j].tree.push_back({ 1, true });
if (i > 0 && j < width - 1)
map[i - 1][j + 1].tree.push_back({ 1,true });
if (j > 0)
map[i][j - 1].tree.push_back({ 1, true });
if (j < width -1)
map[i][j+1].tree.push_back({ 1, true });
if (i < width -1 && j >0)
map[i + 1][j - 1].tree.push_back({ 1,true });
if (i < width -1)
map[i+1][j].tree.push_back({ 1, true });
if (i < width - 1 && j < width -1)
map[i +1][j+1].tree.push_back({ 1, true });
}
}
}
}
}
void winter() {
for(int i = 0; i<width; i++)
for (int j = 0; j < width; j++) {
map[i][j].ntr += nutrition[i][j];
}
}
int checkAliveTree() {
int count = 0;
for (int i = 0; i < width; i++)
for (int j = 0; j < width; j++)
for (int k = 0; k < map[i][j].tree.size(); k++) {
if (map[i][j].tree[k].isAlive)
count++;
else
break;
}
return count;
}