16235 Samsung sw test
์ฝ๋
#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;
}