문제 링크 : https://www.acmicpc.net/problem/16235



문제 접근


1. 각 칸마다 나무가 몇 그루가 들어갈지 모르므로 각 칸마다 ArrayList를 만들어 주어 나무를 추가하였다.


2. 문제 나온대로 봄, 여름, 가을, 겨울 처리를 반복해준 후 살아있는 나무의 수를 구하였다.





소스 코드


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
import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
    
    static int n, m, k, cnt=0;
    static int a[][], yb[][];
    static ArrayList<Namoo> nar[][];
    static int dy[] = { 001-111-1-1 };
    static int dx[] = { 1-1001-11-1 };
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        n = sc.nextInt();
        m = sc.nextInt();
        k = sc.nextInt();
        
        a = new int[n+1][n+1];
        yb = new int[n+1][n+1];
        nar = new ArrayList[n+1][n+1];
        
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                a[i][j] = sc.nextInt();
                yb[i][j] = 5;
                nar[i][j] = new ArrayList<Namoo>();
            }
        }
        
        for(int i=0; i<m; i++) {
            int y = sc.nextInt();
            int x = sc.nextInt();
            int z = sc.nextInt();
            
            nar[y][x].add(new Namoo(z, true));
        }
        
        for(int g=0; g<k; g++) {
            //봄
            for(int i=1; i<=n; i++) {
                for(int j=1; j<=n; j++) {
                    for(int l=0; l<nar[i][j].size(); l++) {
                        if(nar[i][j].get(l).age<=yb[i][j]) {
                            yb[i][j] -= nar[i][j].get(l).age;
                            nar[i][j].get(l).age++;
                        }
                        else {
                            nar[i][j].get(l).alive = false;
                        }
                    }
                }
            }
            //여름
            for(int i=1; i<=n; i++) {
                for(int j=1; j<=n; j++) {
                    for(int l=nar[i][j].size()-1; l>=0; l--) {
                        if(nar[i][j].get(l).alive==false) {
                            yb[i][j] += (nar[i][j].get(l).age/2);
                            nar[i][j].remove(l);
                        }
                    }
                }
            }
            
            //가을
            for(int i=1; i<=n; i++) {
                for(int j=1; j<=n; j++) {
                    for(int l=0; l<nar[i][j].size(); l++) {
                        if(nar[i][j].get(l).age%5==0) {
                            for(int d=0; d<8; d++) {
                                int ny = i + dy[d];
                                int nx = j + dx[d];
                                
                                if(ny<1 || nx<1 || ny>|| nx>n) continue;
                                
                                nar[ny][nx].add(0new Namoo(1true));
                            }
                        }
                    }
                }
            }
            
            //겨울
            for(int i=1; i<=n; i++) {
                for(int j=1; j<=n; j++) {
                    yb[i][j] += a[i][j];
                }
            }
        }
        
        
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                cnt += nar[i][j].size();
            }
        }
        
        System.out.println(cnt);
        
    }
}
 
class Namoo{
    int age;
    boolean alive;
    Namoo(int age, boolean alive) {
        this.age = age;
        this.alive = alive;
    }
}
cs



'Algorithm > 백준' 카테고리의 다른 글

백준 2798번 블랙잭  (0) 2021.08.22
백준 17144번 미세먼지 안녕!  (0) 2019.05.03
백준 15685번 드래곤 커브  (0) 2019.05.02
백준 14891번 톱니바퀴  (0) 2019.04.30
백준 15683번 감시  (0) 2019.04.30

+ Recent posts