Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- binary search
- 시뮬레이션
- COS PRO
- 러닝일지
- greedy
- 문제풀이
- BFS
- BOJ
- MySQL
- 건강
- SWEA
- 문제해결
- 운동기록
- 문자열
- 프로그래머스
- priorityqueue
- dfs
- 알고리즘
- 코테
- 이분탐색
- oracle
- 백준
- db
- math
- 투포인터
- SQL
- Java
- 자바
- DP
- 코딩테스트
Archives
- Today
- Total
슈콩
[BOJ] 백준 16235 나무 재테크 본문
[문제]
https://www.acmicpc.net/problem/16235
[소스 코드]
import java.io.*;
import java.util.*;
public class Main {
static int[] dr = {-1,-1,-1,0,1,1,1,0};
static int[] dc = {-1,0,1,1,1,0,-1,-1};
static class Tree{
int r,c,age;
Tree(int r,int c,int age){
this.r = r;
this.c = c;
this.age = age;
}
}
static int n,k;
static int[][] add;
static int[][] soil;
static int result = 0;
static Queue<Tree> q;
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());
int m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
add = new int[n+1][n+1];
soil = new int[n+1][n+1];
for(int i=1;i<=n;i++) {
st = new StringTokenizer(br.readLine());
for(int j=1;j<=n;j++) {
add[i][j] = Integer.parseInt(st.nextToken());
soil[i][j] = 5;
}
}
q = new LinkedList<>();
for(int i=0;i<m;i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int age = Integer.parseInt(st.nextToken());
q.offer(new Tree(x,y,age));
}
while(k-->0) {
season();
}
result = q.size();
System.out.println(result);
}
private static void season() {
List<Tree> list = new ArrayList<>(q);
list.sort((a,b)-> a.age - b.age);
q = new LinkedList<>();
Queue<Tree> dead = new LinkedList<>();
for(Tree t : list) {
if(soil[t.r][t.c] >= t.age) {
soil[t.r][t.c] -= t.age;
q.offer(new Tree(t.r,t.c,t.age+1));
}
else {
dead.offer(t);
}
}
while(!dead.isEmpty()) {
Tree t = dead.poll();
soil[t.r][t.c] += (t.age / 2);
}
int size = q.size();
for(int i=0;i<size;i++) {
Tree t = q.poll();
if(t.age % 5 == 0) {
for(int d=0;d<8;d++) {
int nr = t.r + dr[d];
int nc = t.c + dc[d];
if(nr<1 || nr>n || nc<1 || nc>n) continue;
q.offer(new Tree(nr,nc,1));
}
}
q.offer(t);
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
soil[i][j] += add[i][j];
}
}
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 백준 16985 Maaaaaaaaaze (0) | 2025.09.25 |
|---|---|
| [BOJ] 백준 16236 아기 상어 (0) | 2025.09.24 |
| [BOJ] 백준 16234 인구 이동 (0) | 2025.09.23 |
| [BOJ] 백준 15686 치킨 배달 (0) | 2025.09.23 |
| [BOJ] 백준 15685 드래곤 커브 (0) | 2025.09.23 |