https://www.acmicpc.net/problem/16235
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int N, M, K;
static LinkedList<Tree> trees;
static Queue<Tree> dead;
static int[][] A, nourishment;
static void input() {
Reader scanner = new Reader();
N = scanner.nextInt();
M = scanner.nextInt();
K = scanner.nextInt();
trees = new LinkedList<>();
dead = new LinkedList<>();
A = new int[N][N];
nourishment = new int[N][N];
for(int row = 0; row < N; row++) {
for(int col = 0; col < N; col++) {
nourishment[row][col] = 5;
A[row][col] = scanner.nextInt();
}
}
for(int count = 0; count < M; count++) {
int x = scanner.nextInt(), y = scanner.nextInt(), age = scanner.nextInt();
trees.add(new Tree(x - 1, y - 1, age));
}
}
static void solution() {
Collections.sort(trees);
for(int year = 1; year <= K; year++) {
spring();
summer();
fall();
winter();
}
System.out.println(trees.size());
}
static void spring() {
LinkedList<Tree> temp = new LinkedList<>();
for(Tree tree : trees) {
if(tree.age <= nourishment[tree.x][tree.y]) {
temp.add(new Tree(tree.x, tree.y, tree.age + 1));
nourishment[tree.x][tree.y] -= tree.age;
} else {
dead.offer(tree);
}
}
trees = temp;
}
static void summer() {
while(!dead.isEmpty()) {
Tree tree = dead.poll();
nourishment[tree.x][tree.y] += (tree.age / 2);
}
}
static void fall() {
int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};
LinkedList<Tree> temp = new LinkedList<>();
for(Tree tree : trees) {
if(tree.age % 5 == 0) {
for(int dir = 0; dir < 8; dir++) {
int cx = tree.x + dx[dir], cy = tree.y + dy[dir];
if(isInMap(cx, cy)) temp.add(new Tree(cx, cy, 1));
}
}
}
trees.addAll(0, temp);
}
static void winter() {
for(int row = 0; row < N; row++) {
for(int col = 0; col < N; col++) nourishment[row][col] += A[row][col];
}
}
static boolean isInMap(int x, int y) {
if(x >= 0 && x < N && y >= 0 && y < N) return true;
return false;
}
public static void main(String[] args) {
input();
solution();
}
static class Tree implements Comparable<Tree> {
int x, y, age;
public Tree(int x, int y, int age) {
this.x = x;
this.y = y;
this.age = age;
}
public int compareTo(Tree t) {
return age - t.age;
}
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}