Home BOJ. Tree Investment (16235)
Post
Cancel

BOJ. Tree Investment (16235)

[Link] https://www.acmicpc.net/problem/16235


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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
int n, m, k;
int add[10][10];
int farm[10][10];
int update[10][10];
int di[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
int dj[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
vector<vector<deque<int>>> tree;

int solve() {
  for0(year, k) {
    for0(row, n) for0(col, n) update[row][col] = 0;
    //spring
    for0(i, n) {
      for0(j, n) {
        int tree_num = tree[i][j].size();
        for0(p, tree_num) {
          if(farm[i][j] < tree[i][j][p]) {
            for0(dd, tree_num - p) {
              int dead_age = tree[i][j].back();
              tree[i][j].pop_back();
              update[i][j] += dead_age >> 1;
            }
            break;
          }
          farm[i][j] -= tree[i][j][p];
          tree[i][j][p]++;
        }
      }
    }
    //summer
    for0(i, n) for0(j, n) farm[i][j] += update[i][j];
    //autumn
    for0(i, n) for0(j, n) {
      for0(p, tree[i][j].size()) {
        if(tree[i][j][p] %5 == 0) {
          for0(p, 8) {
            int ai = i + di[p], aj = j + dj[p];
            if(ai < 0 || aj < 0 || ai >= n || aj >= n) continue;
            tree[ai][aj].push_front(1);
          }
        }
      }
    }
    //winter
    for0(i, n) for0(j, n) farm[i][j] += add[i][j];
  }

  int cnt = 0;
  for0(i, n) for0(j, n) cnt += tree[i][j].size();
  return cnt;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> m >> k;
  for0(i, n) {
    for0(j, n) cin >> add[i][j];
  }
  for0(i, n) for0(j, n) farm[i][j] = 5;
  tree.resize(n);
  for0(i, n) tree[i].resize(n);
  for0(i, m) {
    int x, y, z; cin >> x >> y >> z;
    x--, y--;
    tree[x][y].push_back(z);
  }
  for0(i, n) for0(j, n) sort(tree[i][j].begin(), tree[i][j].end());

  cout << solve();
  return 0;
}
This post is licensed under CC BY 4.0 by the author.