# 유형 : 시뮬레이션

# 난이도 : 골드 V

# 배열이나 리스트 처리만 잘 생각하고 계절에 맞게 구현만 해준다면 어렵지 않은 시뮬레이션 문제였다.

 

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
package bj;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
 
public class p16235 {
    static int N,M,K;
    static int arr[][], map[][];
    static int moveX[] = {0,1,1,1,0,-1,-1,-1};
    static int moveY[] = {-1,-1,0,1,1,1,0,-1};
    static ArrayList<Integer> arrList[][];
    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());
        
        arr = new int[N+1][N+1];
        map = new int[N+1][N+1];
        
        arrList = new ArrayList[N+1][N+1];
        for(int i=1; i<=N; i++)
            for(int j=1; j<=N; j++)
                arrList[i][j] = new ArrayList<>();
        
        for(int i=1; i<=N; i++) {
            for(int j=1; j<=N; j++)
                map[i][j] = 5;
        }
        for(int i=1; i<=N; i++) {
            st = new StringTokenizer(br.readLine());
            for(int j=1; j<=N; j++
                arr[i][j] = Integer.parseInt(st.nextToken());
                
        }
        for(int m=1; m<=M; m++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());
            int age = Integer.parseInt(st.nextToken());
            arrList[u][v].add(age);
        }
        int k = 0;
        while(k<K) {
            
            // 봄 + 여름
            for(int i=1; i<=N; i++) {
                for(int j=1; j<=N; j++) {
                    if(arrList[i][j].size() > 0) {
                        int end = 0;
                        Collections.sort(arrList[i][j]);
                        
                        ArrayList<Integer> tmp = new ArrayList<>();
                        for(int q=0; q<arrList[i][j].size(); q++) {
                            int age = arrList[i][j].get(q);
                            if(map[i][j] >= age) {
                                map[i][j] -= age;
                                tmp.add(age+1);
                            }else {
                                end += age/2;
                            }
                        }
                        arrList[i][j] = new ArrayList<>();
                        for(int val : tmp)
                            arrList[i][j].add(val);
                        
                        map[i][j] += end;
                    }
                }
            }
            
            /// 가을
            for(int i=1; i<=N; i++) {
                for(int j=1; j<=N; j++) {
                    if(arrList[i][j].size() > 0) {
                        
                        for(int q=0; q<arrList[i][j].size(); q++) {
                            int age = arrList[i][j].get(q);
                            if(age % 5 == 0) {
                                for(int d=0; d<8; d++) {
                                    int newX = j + moveX[d];
                                    int newY = i + moveY[d];
                                    if(0<newX && newX<=&& 0<newY && newY<=N) {
                                        arrList[newY][newX].add(1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
            /// 겨울
            for(int i=1; i<=N; i++)
                for(int j=1; j<=N; j++)
                    map[i][j] += arr[i][j];
            
            
            k++;
        }
        int result = 0;
        for(int i=1; i<=N; i++) {
            for(int j=1; j<=N; j++) {
                result += arrList[i][j].size();
            }
        }
        System.out.println(result);
    }
}
    
 
cs

+ Recent posts