๋ฌธ์ ๋ฅผ ์ ์ฝ์ผ๋ฉด์ ๋ด, ์ฌ๋ฆ, ๊ฐ์, ๊ฒจ์ธ์ ํ๋์ฉ ๊ตฌํํด๋ณด๋ฉด ๊ตฌํ ์์ฒด๋ ์ฝ๋ค.
์ด ๋ฌธ์ ์์ ๊ฐ์ฅ ์ด๋ ค์ด ์ ์ 0.3์ด๋ผ๋ ์๊ฐ์ ํ์ ๋ซ๊ธฐ์ํด ์ด๋ป๊ฒ๋ ์๊ฐ์ ์ค์ฌ์ผ ํ๋ค๋ ์ ์ด๋ค. ๊ทธ๋์ ๋ค์์ ์ด๋ฌํ ๊ตฌํ ๋ฌธ์ ๋ฅผ ํ ๋ ๋์๋ ๋ ๊ฒธ ๋ฌธ์ ๋ฅผ ํ๋ฉด์ ์ฐพ์ ์๊ฐ์ ์ค์ด๋ ๋ฐฉ๋ฒ๋ค์ ์ ๋ฆฌํด๋ณด๋ ๊ฒ์ด๋ค.
์ฒ์์ ์ฝ๋๋ ์ด๋ ๊ฒ ๊ตฌ์ฑํ๋ค.
for (int year = 0; year < k; year++) {
if (trees.isEmpty()) break;
trees.sort(new Comparator<tree>() {
@Override
public int compare(tree o1, tree o2) {
return o1.old - o2.old;
}
});
// spring
ArrayDeque<tree> stk = new ArrayDeque<>();
for (tree t : trees) {
if (map[t.y][t.x] >= t.old) {
map[t.y][t.x] -= t.old;
t.old++;
} else stk.push(t);
}
// summer
while (!stk.isEmpty()) {
tree t = stk.pop();
trees.remove(t);
map[t.y][t.x] += (t.old / 2);
}
// autumn
ArrayList<tree> tmp = new ArrayList<>();
int size = trees.size();
for (int i = 0; i < size; i++) {
tree t = trees.get(i);
if (t.old % 5 == 0) {
for (int d = 0; d < 8; d++) {
int ny = t.y + dy[d];
int nx = t.x + dx[d];
if (ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
tmp.add(new tree(ny, nx, 1));
}
}
}
trees.addAll(tmp);
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
map[y][x] += A[y][x];
}
}
}
์ฝ๋๋ฅผ ๋ถ์ํด๋ณด๋ฉด, ๋ด, ์ฌ๋ฆ, ๊ฐ์, ๊ฒจ์ธ์ ํ๋์ฉ ๊ตฌํํ์๊ณ ์ด๋ฅผ year๊ฐ 0๋ถํฐ ์ฆ๊ฐํ๋ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ฐ์ธ ๋ชจ๋ ๋ก์ง์ด ์ํํ๋ฉด 1๋ ์ด ์ง๋๋๋ก ํ์๋ค.
๋ด ์ฝ๋๋ฅผ ๋ณด๋ฉด, ๋ด์์๋ ๋๋ฌด์ ์ ๋ณด๋ฅผ ํ๋์ฉ ๊บผ๋ด์ ์์ ์ ๋์ด๋งํผ ์๋ถ์ ๋จน๊ณ , ๋์ด๊ฐ 1 ์ฆ๊ฐํ๊ฑฐ๋ ํน์ ์๋ถ์ด ๋ถ์กฑํด์ ์ฃฝ๋๋ค. ์ด๋, ์ฌ๋ฌ ๋๋ฌด๊ฐ ํ ์ขํ์ ์์ผ๋ฉด ๋์ด๊ฐ ์ด๋ฆฐ ๋๋ฌด๋ถํฐ ์๋ถ์ ๋จน๋ ์กฐ๊ฑด์ด ์๊ธฐ ๋๋ฌธ์ ๋ฏธ๋ฆฌ ๋๋ฌด ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌํด์ฃผ์๋ค. ๋, ์ฃฝ์ ๋๋ฌด๋ ์ฌ๋ฆ์์ ์ฌ์ฉํด์ผ ํ๊ธฐ๋๋ฌธ์ ์คํ์ ์ฃฝ์ ๋๋ฌด๋ค์ ๋ด์์ค๋ค.
์ฌ๋ฆ์๋ ์คํ์ ๋ด์๋์๋ ์ฃฝ์ ๋๋ฌด๋ค์ ํ๋์ฉ ๊บผ๋ด๋ฉด์ ๋๋ฌด ๋ฆฌ์คํธ์์ ํด๋น ๋๋ฌด๋ค์ ์ง์ฐ๊ณ , ๋์ด / 2๋งํผ์ map์ ๋ํด์ค๋ค.
๊ฐ์์๋ tmp๋ผ๋ ์๋กญ๊ฒ ์๋ผ๋๋ ๋๋ฌด๋ค์ ๋ด์ ๋ฐฐ์ด์ ์ ์ธํด์ฃผ๊ณ , ๋๋ฌด ๋ฆฌ์คํธ๋ฅผ ํ๋์ฉ ๋ฐฉ๋ฌธํ๋ฉด์ 5์ ๋ฐฐ์์ด๋ฉด 8๋ฐฉํฅ์ ์ขํ์ ๋๋ฌด๋ค์ ์ถ๊ฐํด์ค๋ค. ๋ชจ๋ ๋๋ฌด๋ฅผ ๋ฐฉ๋ฌธํ์ผ๋ฉด ๋ค์ tress.addAll(tmp);๋ก ์๋กญ๊ฒ ์๋ผ๋๋ ๋๋ฌด๋ค์ ๋ฆฌ์คํธ์ ์ถ๊ฐํด์ค๋ค.
๊ฒจ์ธ์๋ ๋จ์ํ ๋ชจ๋ ๋งต์๋ค A ๋ฐฐ์ด์ ๊ฐ์ ๊ฐ๊ฐ ๋ํด์ค๋ค.
๋ฑ๋ณด์๋ ๊ตฌํ์ ์ง์คํ์ฌ ์๋นํ ๋นํจ์จ์ ์ธ ์คํ์ ๋ณด์ฌ์ค๋ค. ๋น์ฐํ๊ฒ๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค. ๊ทธ๋ผ ๋ฌธ์ ๋ฅผ ํตํด ์ด๋ป๊ฒ ์๊ฐ์ ์ค์ผ ์ ์์์ง ๊ณ ๋ฏผํด๋ณด์. ์ผ๋จ, ๋ฌธ์ ์ ๋ด, ์ฌ๋ฆ, ๊ฐ์, ๊ฒจ์ธ ๋ด์ฉ์ ํ๋ฒ๋ณด์.
๋ด์๋ ๋๋ฌด๊ฐ ์๋ถ์ ๋จน๊ณ ๋์ด๊ฐ ์ฆ๊ฐํ๋ฉฐ, ์ฌ๋ฆ์๋ ์ฃฝ์ ๋๋ฌด๋ค์ ์๋ถ์ด ๋งต์ ๋ํด์ง๋ค. ๊ฐ์์๋ ๋ฒ์๋ง ํ๋ค. ๊ฒจ์ธ์๋ ๋งต์ ์๋ถ๋ค์ด ๋ํด์ง๋ค. ์ด๋ ๊ฒ ๋ณด๋ฉด ๋งต์ ๊ธฐ์ค์ผ๋ก ๋ด์๋ ์๋ถ์ด ์ค์ด๋ค๊ณ , ์ฌ๋ฆ๊ณผ ๊ฒจ์ธ์๋ ์๋ถ์ด ์ฆ๊ฐํ๋ฉฐ ๊ฐ์์๋ ๋ณํ๊ฐ ์๋ค. ๊ทธ๋ผ ๋ด ์ดํ์ ์ฌ๋ฆ๊ณผ ๊ฒจ์ธ์ ๋ฌถ์ด ์ฒ๋ฆฌํ๊ณ ๊ฐ์์ ์ด๋ค ๊ฒฝ์ฐ์ ์งํ๋์ด๋ ๋ฌธ์ ๊ฐ ์์ง ์์๊น?
์ด๋ฌํ ์๊ฐ์ ๊ฐ๊ณ ํ๋ฒ ์ ๊ทผํด๋ณด์. ์ผ๋จ ์ฌ๋ฆ๊ณผ ๊ฒจ์ธ์ ํ๋์ ๋ก์ง์ผ๋ก ์ฒ๋ฆฌํ์. ๊ฒจ์ธ๋ถํฐ๋ณด๋ฉด ๊ฒจ์ธ์๋ ๋ฌด์กฐ๊ฑด S2D2๊ฐ ๋ชจ๋ ๋ ์ ๋์๋ค๋๋ฉฐ ์๋ถ์ ๋ํด์ผํ๋๊น ์์๋ฅผ ํ๋ํ๋ ์ ๊ทผํ๋ ์ฝ๋์์ ๋ ์ค์ผ ์๋ ์์ ๊ฒ ๊ฐ๋ค. ๊ทธ๋ผ ์ฌ๋ฆ์ ์ค์ฌ์ผํ๋ค. ์ด๋ฅผ ์ํด ์ฌ๋ฆ๋ ๊ฒจ์ธ๊ณผ ๋์ผํ๊ฒ n * n ํฌ๊ธฐ์ 2์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ์ฃฝ์ ๋๋ฌด ์ขํ๋ง๋ค old / 2๋ฅผ ๋ํด๋๊ณ ใ กํ๋์ ์ขํ์๋ ์ฌ๋ฌ ๊ฐ์ ๋๋ฌด๊ฐ ์์ ์ ์์ผ๋ฏ๋ก ์ฌ๋ฌ ๊ฐ์ ๋๋ฌด๊ฐ ์ฃฝ์์ ๊ฒฝ์ฐ๋ฅผ ์ํด ๋ํ๊ธฐ๋ฅผ ํ๋ค.ใ ก, ๊ฒจ์ธ๊ณผ ๊ฐ์ด map์๋ค๊ฐ ๋ํ๋ฉด ๋์ง ์์๊น?
์ฆ, ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ ๊ฒ ๊ฐ๋ค.
// dies๋ 0์ผ๋ก ์ด๊ธฐํ
...
// ์ฃฝ์ ๋๋ฌด๋ฅผ ์ฐพ๋ ๋ก์ง
dies[y][x] += (old / 2);
...
// ์ฌ๋ฆ๊ณผ ๊ฒจ์ธ์ ๋์์ ์งํ
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
map[y][x] += A[y][x] + dies[y][x];
dies[y][x] = 0; // 0์ผ๋ก ์ด๊ธฐํ ํด์ค์ผ ๋ค์ ๋
๋์ ์ํฅ์ ๋ผ์น์ง ์์
}
}
์ฃฝ์ ๋๋ฌด๋ฅผ ์ฐพ๋ ๋ก์ง์ ๊ฒฐ๊ตญ ๋ด์์ ํด์ผํ๋ฏ๋ก ๋ด์ด ์งํํ๋ฉฐ map[t.y][t.x] < t.old๋ฉด dies[t.y][t.x] += (t.old / 2)๋ฅผ ํตํด ํด๋น ๊ฐ๋ค ์ ์ฅํ ์ ์์ ๊ฒ ๊ฐ๋ค. ๊ทธ๋ผ ์ด์ ๋ด์ ํ๋ฒ ๋ฐ๊พธ์ด๋ณด์.
trees.sort(new Comparator<tree>() {
@Override
public int compare(tree o1, tree o2) {
return o1.old - o2.old;
}
});
ArrayDeque<tree> stk = new ArrayDeque<>();
for (tree t : trees) {
if (map[t.y][t.x] >= t.old) {
map[t.y][t.x] -= t.old;
t.old++;
} else stk.push(t);
}
์ผ๋จ ์คํ์ ์ด์ ํ์๊ฐ ์๋ค. ์ฌ๋ฆ ์ฝ๋ ์์ฒด๋ฅผ ๊ฒจ์ธ๊ณผ ํฉ์ณค๊ธฐ ๋๋ฌธ์ด๋ค. ๊ทธ๋ผ ์ด๋ป๊ฒ ๋๋ฌด ๋ฆฌ์คํธ์์ ํด๋น ๋๋ฌด๋ค์ ์ง์ธ ์ ์์๊น? ์ด๋ ๊ฒ์์ ํตํด์ ์์๋๋ฐ Iterator๋ฅผ ํตํ๋ฉด iter.remove()๋ฅผ ํตํด ํ์ฌ ์งํ ์ค์ธ ์์๋ฅผ ์๋ณธ ๋ฆฌ์คํธ์์ ์ง์ธ ์ ์๋ค. ์ฌ๊ธฐ๊น์งํ๋ฉด ๋์ถฉ ์ด๋ฐ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ฒ ๋ค.
trees.sort(new Comparator<tree>() {
@Override
public int compare(tree o1, tree o2) {
return o1.old - o2.old;
}
});
Iterator iter = trees.iterator();
while (iter.hasNext()) {
tree t = (tree)iter.next();
if (map[t.y][t.x] >= t.old) {
map[t.y][t.x] -= t.old;
t.old++;
} else {
dies[t.y][t.x] += (t.old / 2);
iter.remove();
}
}
๊ทผ๋ฐ Iterator๋ ArrayList๋ณด๋ค LinkedList์์ ๋ ํจ์จ์ ์ด๋ค. ๊ทธ๋ฆฌ๊ณ , ์ญ์ ๊ณผ์ ์์ iter๋ฅผ ํตํด remove๋ฅผ ํ๋ฉด ArrayList๋ ๋ค์ ์๋ ๋๋ฌด๋ค์ ์์ผ๋ก ๋น๊ธฐ๋ ์ํํธ ์์ ์ ํด์ผํด์ O(n)์ ์๊ฐ ๋ณต์ก๋๊ฐ ์๊ธฐ์ง๋ง LinkedList๋ ๋ฆฌ์คํธ ์ฐ๊ฒฐ ๋ฐ ์ญ์ ๋ง ํด์ฃผ๋ฉด ๋๋ฏ๋ก O(1)์ด๋ค. LinkedList๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์คํ๋ ค ํ๋นํ ๊ฒ์ด๋ค. ๋ด์ ์ด์ ๋๋ก๋ง ํ๊ณ ๊ฐ์์ ํ๋ฒ ์๊ฐํด๋ณด์.
๊ฐ์์ ๋๋ฌด๊ฐ ๋ฒ์ํ๋ค. ๋ด์ ์งํํ๊ณ ํ์ํ๋ฉด์ ๋ฒ์์ ํ๋ฉด ๋ ๊น? ๊ทธ๊ฒ๋ณด๋ค๋ ๋ด์ด ์งํํ๋ฉด์ ๋๋ฌด๊ฐ ๋์ด๋ฅผ ๋จน์๋ค๋ฉด, ๊ฑ๋ฅผ ๋ฐ๋ก ๋ฒ์์ํค๋ฉด ๋๋์ฑ ๋ก์ง์ ์๊ฐ์ ์ค์ผ ์ ์์ง ์์๊น? ๋ผ๋ ์๊ฐ์ผ๋ก ์๊ฐ์ ์ค์ฌ๋ณด์.
๋ด์์ ๋์ด๋ฅผ ๋จน๋ ์ํฉ์ if (map[t.y][t.x] >= t.old) ์ด ๋ถ๊ธฐ๋ฌธ ์ดํ์ด๋ค. ๊ทธ๋ผ ํด๋น ๋ถ๊ธฐ๋ฌธ์ ๋์ด๋ฅผ ๋จน๊ณ , ๋ฐ๋ก ๊ฐ์์์์ ๋ฒ์์ ํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
trees.sort(new Comparator<tree>() {
@Override
public int compare(tree o1, tree o2) {
return o1.old - o2.old;
}
});
LinkedList<tree> tmp = new LinkedList<>();
Iterator iterator = trees.iterator();
while (iterator.hasNext()) {
tree t = (tree) iterator.next();
// ์๋ถ์ ๋จน๊ณ ๋์ด ์ฆ๊ฐ -> ๋ฒ์
if (map[t.y][t.x] >= t.old) {
map[t.y][t.x] -= t.old;
t.old++;
if (t.old % 5 == 0) {
for (int d = 0; d < 8; d++) {
int ny = t.y + dy[d];
int nx = t.x + dx[d];
if (ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
tmp.add(new tree(ny, nx, 1));
}
}
} else {
dies[t.y][t.x] += t.old/2;
iterator.remove();
}
}
trees.addAll(tmp);
๋ง์ง๋ง์ผ๋ก addAll์๋ ์ธ๋ฑ์ค๋ฅผ ์ง์ ํ์ฌ ์ถ๊ฐํ ์ ์๋ ๊ธฐ๋ฅ์ด ์๋ค. ์ ์ด์ ์๋ผ๋๋ ๋๋ฌด๋ค์ ๋ฌด์กฐ๊ฑด 0๋ฒ ์ธ๋ฑ์ค๋ถํฐ ์ถ๊ฐํ๋ฉด ์ ๋ ฌํ ํ์์์ด ๋ฌด์กฐ๊ฑด ์ด๋ฆฐ ๋์ด์ ๋๋ฌด๋ค์ด ์์ ์๊ฒ ๋๋ค. ๊ทธ๋ผ ์ต์ข ์ ์ผ๋ก ๋ด๊ณผ ๊ฐ์์ ๋ค์๊ณผ ๊ฐ๋ค.
LinkedList<tree> tmp = new LinkedList<>();
Iterator iterator = trees.iterator();
while (iterator.hasNext()) {
tree t = (tree) iterator.next();
// ์๋ถ์ ๋จน๊ณ ๋์ด ์ฆ๊ฐ -> ๋ฒ์
if (map[t.y][t.x] >= t.old) {
map[t.y][t.x] -= t.old;
t.old++;
if (t.old % 5 == 0) {
for (int d = 0; d < 8; d++) {
int ny = t.y + dy[d];
int nx = t.x + dx[d];
if (ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
tmp.add(new tree(ny, nx, 1));
}
}
} else {
dies[t.y][t.x] += t.old/2;
iterator.remove();
}
}
trees.addAll(0, tmp);
์ฝ๋
import java.io.*;
import java.util.*;
public class Main {
static int n, m, k;
static int[][] map;
static int[][] dies;
static int[][] A;
static LinkedList<tree> trees = new LinkedList<>();
static int[] dy = {-1, -1, -1, 0, 0, 1, 1, 1};
static int[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
static class tree {
int x, y, old;
tree(int y, int x, int old) {
this.y = y;
this.x = x;
this.old = old;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
map = new int[n][n];
dies = new int[n][n];
A = new int[n][n];
for (int y = 0; y < n; y++) {
st = new StringTokenizer(br.readLine());
for (int x = 0; x < n; x++) {
map[y][x] = 5;
A[y][x] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int old = Integer.parseInt(st.nextToken());
trees.add(new tree(y-1, x-1, old));
}
// ์ด๊ธฐ์๋ ๋๋ฌด์ ๋์ด๊ฐ ๋ฌด์์๋ก ์ฃผ์ด์ง๋ฏ๋ก ํ๋ฒ์ ์ ๋ ฌ์ด ํ์ํ๋ค.
trees.sort(new Comparator<tree>() {
@Override
public int compare(tree o1, tree o2) {
return o1.old - o2.old;
}
});
for (int year = 0; year < k; year++) {
if (trees.isEmpty()) break;
// ๋ด ๊ฐ์
LinkedList<tree> tmp = new LinkedList<>();
Iterator iterator = trees.iterator();
while (iterator.hasNext()) {
tree t = (tree) iterator.next();
// ์๋ถ์ ๋จน๊ณ ๋์ด ์ฆ๊ฐ -> ๋ฒ์
if (map[t.y][t.x] >= t.old) {
map[t.y][t.x] -= t.old;
t.old++;
if (t.old % 5 == 0) {
for (int d = 0; d < 8; d++) {
int ny = t.y + dy[d];
int nx = t.x + dx[d];
if (ny < 0 || nx < 0 || ny >= n || nx >= n) continue;
tmp.add(new tree(ny, nx, 1));
}
}
} else {
dies[t.y][t.x] += t.old/2;
iterator.remove();
}
}
trees.addAll(0, tmp);
// ์ฌ๋ฆ, ๊ฒจ์ธ
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
map[y][x] += dies[y][x] + A[y][x];
dies[y][x] = 0;
}
}
}
System.out.println(trees.size());
}
}
'์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก์ด๋ ์์ ์๊ณ ๋ฆฌ์ฆ (0) | 2025.01.17 |
---|---|
ํ์ ํธ๋ฆฌ (1) | 2024.10.03 |
1513 ๋ฌธ์ ์์ ์ 4์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํด์ผ ํ๋? (0) | 2024.09.25 |
2293 - ๋์ 1 (0) | 2024.09.23 |
2294๋ฒ ๋ฌธ์ ๋ฉ๋ชจ (0) | 2024.09.22 |