Post

SWEA_5653_줄기세포 배양 (Java)

SWEA_5653_줄기세포 배양 (Java)

[Unrated] [모의 SW 역량테스트] 줄기세포배양 - 5653

문제 링크

성능 요약

메모리: 100,876 KB, 시간: 2,361 ms, 코드길이: 3,798 Bytes

제출 일자

2025-02-11 19:24

출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do

문제 풀이

Cell 클래스로 위치, 에너지, 시간, 세포타입을 관리했다. 우선순위는 에너지가 큰 순으로 내림차순이다.

이를 구현하면 된다.

코드

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
/**
 * Author: nowalex322, Kim HyeonJae
 */
 
import java.io.*;
import java.util.*;
 
public class Solution {
    class Cell implements Comparable<Cell> {
        int r, c, energy, time, type;
        public Cell(int r, int c, int energy, int time, int type) {
            this.r = r;
            this.c = c;
            this.energy = energy;
            this.time = time;
            this.type = type; // 0:비활성, 1:활성, 2:죽음
        }
 
        @Override
        public int compareTo(Cell o){
            return o.energy - this.energy;
        }
 
    }
    static BufferedReader br;
    static BufferedWriter bw;
    static StringTokenizer st;
    static StringBuilder sb = new StringBuilder();
    static int T, N, M, K;
    static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
    public static void main(String[] args) throws Exception {
        new Solution().solution();
    }
 
    public void solution() throws Exception {
        br = new BufferedReader(new InputStreamReader(System.in));
//        br = new BufferedReader(new InputStreamReader(new FileInputStream("src/main/java/SWEA_5653_줄기세포배양/input.txt")));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
         
        T = Integer.parseInt(br.readLine());
        for(int tc=1; tc<=T; tc++) {
            st = new StringTokenizer(br.readLine());
            N = Integer.parseInt(st.nextToken());
            M = Integer.parseInt(st.nextToken());
            K = Integer.parseInt(st.nextToken());
 
            int maxSize = Math.max(N, M) + K * 2;
            Cell[][] board = new Cell[maxSize][maxSize];
            for(int i=0; i<N; i++){
                st = new StringTokenizer(br.readLine());
                for(int j=0; j<M; j++){
                    int energy = Integer.parseInt(st.nextToken());
                    if(energy > 0) board[i+K][j+K] = new Cell(i+K, j+K, energy, 0, 0);
                }
            }
            for(int t=0; t<K; t++){
                List<Cell> growingCells = new ArrayList<Cell>();
                for(int i=0; i<maxSize; i++){
                    for(int j=0; j<maxSize; j++){
                        if(board[i][j] == null || board[i][j].type == 2) continue;
 
                        Cell currCell = board[i][j];
                        currCell.time++;
 
                        if(currCell.type == 0 && currCell.time == currCell.energy){
                            currCell.type++;
                            currCell.time = 0;
                        }
 
                        if(currCell.type == 1) {
                            if(currCell.time == 1){
                                for(int k=0; k<4; k++){
                                    int nr = currCell.r + dr[k];
                                    int nc = currCell.c + dc[k];
                                    if(board[nr][nc] == null) {
                                        growingCells.add(new Cell(nr, nc, currCell.energy, 0, 0));
                                    }
                                }
                            }
                            if(currCell.time == currCell.energy) currCell.type++;
                        }
                    }
                }
                Collections.sort(growingCells);
                for(Cell c : growingCells){
                    if(board[c.r][c.c] == null) board[c.r][c.c] = c;
                }
            }
 
            int res = 0;
            for(int i=0; i<maxSize; i++){
                for(int j=0; j<maxSize; j++){
                    if(board[i][j] != null && board[i][j].type != 2) res++;
                }
            }
            sb.append("#").append(tc).append(" ").append(res).append("\n");
        }
        bw.write(sb.toString());
        bw.flush();
        bw.close();
        br.close();
    }
}
This post is licensed under CC BY 4.0 by the author.