문제보기 >> [BOJ] 16235 : 나무재테크
![](http://www.businessplus.kr/news/photo/201511/1407_624_186.jpg)
Comment
문제 그대로 구현했다.
우선순위큐를 사용할때 팝하는 과정에서 내 의지와 다르게 팝될 수 있으므로 주의 ..**
우선순위큐 사이즈만큼 팝하고, 다시 애드 할때에는 새로운 큐에 담은 후 옮겨줘야한다
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
//BOJ :: 16235 나무재테크
//2019-04-13
public class Main16235_나무재테크 {
private static int[][] A;
private static int N,K;
private static PriorityQueue<pair> trees;
private static int[][] Y;
private static int[] dy = {-1,-1,-1,0,0,1,1,1};
private static int[] dx = {-1,0,1,-1,1,-1,0,1};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
A = new int[N + 1][N + 1];
Y = new int[N + 1][N + 1];
trees = new PriorityQueue<>();
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 1; j <= N; j++) {
A[i][j] = Integer.parseInt(st.nextToken());
Arrays.fill(Y[i], 5); // 양분 초기값
}
}
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 z = Integer.parseInt(st.nextToken()); // 나무 나이
trees.add(new pair(y, x, z));
}
//////// input
treeInvestment();
System.out.println(trees.size());
}
public static void treeInvestment() {
ArrayList<pair> die = new ArrayList<>();
ArrayList<pair> breed = new ArrayList<>();
for (int year = 0; year < K; year++) {
// 봄
int tsize = trees.size();
PriorityQueue<pair> newpq = new PriorityQueue<>();
for (int i = 0; i < tsize; i++) {
pair p = trees.poll();
int py = p.y;
int px = p.x;
if (Y[py][px] < p.age) {// 양분이 내 나이보다 적으면 죽는다.
die.add(new pair(py,px,p.age));
continue;
}
Y[py][px] -= p.age; // 나이만큼 양분 먹기
newpq.add(new pair(py, px, p.age+1));
if((p.age+1)%5==0) {//나이가 5의 배수면
breed.add(new pair(py,px,p.age+1));
}
}
trees = new PriorityQueue<>(newpq);
// 여름
for(pair p : die) {
int py = p.y;
int px = p.x;
Y[py][px] += p.age/2;
}
die.clear();
//가을
for(pair p : breed) {
int py = p.y;
int px=p.x;
for(int i=0;i<8;i++) {
int ny = py+dy[i];
int nx = px+dx[i];
if(ny<1 || ny>N || nx<1 || nx>N) continue;
trees.add(new pair(ny,nx,1));
}
}
breed.clear();
//겨울
for(int i=1;i<=N;i++) {
for(int j=1;j<=N;j++) {
Y[i][j] += A[i][j];
}
}
}//year term
return;
}
static class pair implements Comparable<pair> {
int y;
int x;
int age;
public pair(int y, int x, int age) {
super();
this.y = y;
this.x = x;
this.age = age;
}
@Override
public int compareTo(pair o) {
return this.age - o.age;
}
}
}
|
cs |
'Algorithm Problem Solving' 카테고리의 다른 글
[BOJ] 17281 :: ⚾ 야구 (2) | 2019.06.05 |
---|---|
[BOJ] 16236 : 아기상어 (0) | 2019.06.05 |
[BOJ] 15685. 드래곤 커브 (5) | 2019.04.09 |
[BOJ] 1938 통나무 옮기기 (0) | 2019.04.05 |
[SWEA] 2383. 점심식사 시간 (5) | 2019.04.04 |