Skip to content

Instantly share code, notes, and snippets.

@Bloofer
Created April 21, 2021 13:05
Show Gist options
  • Select an option

  • Save Bloofer/bc4cbb849457d6e9aedaeec629cdc35f to your computer and use it in GitHub Desktop.

Select an option

Save Bloofer/bc4cbb849457d6e9aedaeec629cdc35f to your computer and use it in GitHub Desktop.
trees
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M, K;
int A[10][10];
int land[10][10]; // 땅의 양분 정보
typedef struct{
int year;
bool dead;
}TREE;
vector<TREE> tree[10][10]; // 땅의 나무 정보
int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool cmp_tree(TREE t1, TREE t2){ return t1.year < t2.year; }
bool available(int x, int y){ return x >= 0 && y >= 0 && x < N && y < N; }
void sort_trees(){
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sort(tree[i][j].begin(), tree[i][j].end(), cmp_tree);
}
}
}
void year_pass(){ // 1년 동안 나무의 사계절 수행 함수.
// 1. 봄: 나무가 자신 나이만큼 양분먹고, 나이 1 증가
// 나무 여러그루 시, 어린나무부터 먹음
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < tree[i][j].size(); k++) {
if(land[i][j] >= tree[i][j][k].year) {
// 양분 충분시 land에 해당 나무 나이만큼 양분 줄임
land[i][j] -= tree[i][j][k].year;
tree[i][j][k].year++;
} else {
// 양분 부족시 dead에 죽는 나무 마킹
tree[i][j][k].dead = true;
}
}
}
}
// 2. 여름: 죽는나무 -> 양분
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
vector<TREE> tmp;
for (int k = 0; k < tree[i][j].size(); k++) {
if(tree[i][j][k].dead) {
// 죽은 나무 나이 / 2 => 칸에 양분 추가
land[i][j] += tree[i][j][k].year / 2;
}
else {
// 산 나무는 그대로 배열 옯겨서 tree에 유지
tmp.push_back(tree[i][j][k]);
}
}
tree[i][j] = tmp;
}
}
// 3. 가을: 5배수 나무 나이 번식, 인접 8칸에 나무 나이 1생김
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < tree[i][j].size(); k++) {
if((tree[i][j][k].year%5)==0) {
for (int p = 0; p < 8; p++) {
int nx = i + dx[p];
int ny = j + dy[p];
if(available(nx, ny)) tree[nx][ny].push_back(TREE{1, false});
}
}
}
}
}
// 가을 수행 후 tree[][] 배열 전체 정렬
sort_trees();
// 4. 겨울: 땅에 A[][] 양분 추가
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
land[i][j] += A[i][j];
}
}
}
int main(){
cin >> N >> M >> K;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> A[i][j];
land[i][j] = 5;
}
}
for (int i = 0; i < M; i++) {
int x, y, z;
cin >> x >> y >> z;
TREE t;
t.year = z; t.dead = false;
tree[x-1][y-1].push_back(t);
}
sort_trees();
for (int i = 0; i < K; i++) year_pass();
int ans = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ans += tree[i][j].size();
}
}
cout << ans << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment