본문 바로가기

알고리즘

줄기세포 배양 [swexpert academy]

링크 : https://swexpertacademy.com/main/code/problem/problemDetail.do

 

포인트

1]2차원 배열 복사 (배열을 2개 써야 하는 이유 잘 곱씹어 볼 수 있는 문제, '목수의 미로 탈출' 문제를 연상하면 될 것 같다.

2]안전하게 좌표 처리,

3]시간 효율성을 위해 전부다 loop를 도는 것이 아니라 필요한 만큼만 도는 것.

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package academymoi;
 
import java.util.Scanner;
 
public class 줄기세포배양 {
    static int ans;
    static int t, n, m, time;
    static Cell[][] map, next;
    static int[] dx = {0,1,0,-1};
    static int[] dy = {1,0,-1,0};
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        t = scan.nextInt();
        for (int i = 1; i <= t; i++) {
            input(scan);
            solve();
            count();
            System.out.println("#"+ i+" "+ans);
            ans = 0;
        }
    }
    
    private static void count() 
    {
        int cnt=0;
        for(int i=0;i<400;i++
        {
            for(int j=0;j<400;j++
            {
                if(map[i][j].status==1 || map[i][j].status==2
                {
                    cnt+=1;
                }
            }
        }
        ans = cnt;
    }
    
    private static void solve() {
        
        for(int a=1;a<=time;a++
        {
            for(int i=200-time/2-2;i<=200+n+time/2+2;i++
            {
                for(int j=200-time/2-2;j<=200+m+time/2+2;j++
                {
                    if(map[i][j].status!=0
                    {
                        if(map[i][j].status==3
                        {
                            next[i][j].status=3;
                            next[i][j].total = map[i][j].total;
                            next[i][j].current=0;
                            continue;
                        }
                        else if(map[i][j].status==1
                        {
                            next[i][j].current = map[i][j].current+1;
                            next[i][j].total = map[i][j].total;
                            if(next[i][j].current==next[i][j].total) 
                            {
                                next[i][j].status=2;
                            }
                            else 
                            {
                                next[i][j].status=1;
                            }
                        }
                        else if(map[i][j].status==2
                        {
                            //처음인 경우 확장
                            if(map[i][j].total==map[i][j].current) 
                            {
                                int nx;
                                int ny;
                                for(int k=0;k<4;k++
                                {
                                    nx = i+dx[k];
                                    ny = j+dy[k];
                                    
                                    if(map[nx][ny].status==0
                                    {
                                        if(next[nx][ny].status==0
                                        {
                                            next[nx][ny].status=1;
                                            next[nx][ny].total = map[i][j].total;
                                            next[nx][ny].current=0;
                                        }
                                        else if(next[nx][ny].status==1
                                        {
                                            if(next[nx][ny].total < map[i][j].total) 
                                            {
                                                next[nx][ny].total = map[i][j].total;
                                                next[nx][ny].current=0;
                                                next[nx][ny].status=1;
                                            }
                                        }
                                    }
                                }
                            }
                            // 1개 감소 -> 체력 깍기
                            next[i][j].current = map[i][j].current-1;
                            if(next[i][j].current==0
                            {
                                next[i][j].status=3;
                                next[i][j].total=map[i][j].total;
                                next[i][j].current=0;
                            }
                            else 
                            {
                                next[i][j].status=2;
                                next[i][j].total=map[i][j].total;
                            }
                        }
                    }
                }
            }
            
            // next -> map
            for(int i=200-time/2-2;i<=200+n+time/2+2;i++
            {
                for(int j=200-time/2-2;j<=200+m+time/2+2;j++
                {
                    map[i][j].init();
                    map[i][j].set(next[i][j]);
                    next[i][j].init();
                }
            }
            
        }
        
    }
 
    private static void input(Scanner scan) {
        init();
        n = scan.nextInt();
        m = scan.nextInt();
        time = scan.nextInt();
        
        
        for (int i = 200; i < 200 + n; i++
        {
            for (int j = 200; j < 200 + m; j++
            {
                int v = scan.nextInt();
                if (v != 0
                {
                    map[i][j].total = v;
                    map[i][j].status = 1;
                }
            }
        }
 
    }
 
    public static void init() {
        map = new Cell[400][400];
        next = new Cell[400][400];
        for (int i = 0; i < 400; i++) {
            for (int j = 0; j < 400; j++) {
                map[i][j] = new Cell();
                map[i][j].init();
                next[i][j] = new Cell();
                next[i][j].init();
            }
        }
    }
}
 
class Cell {
    int status;
    int total;
    int current;
 
    public void init() {
        status = 0;
        total = 0;
        current = 0;
    }
 
    public void set(Cell cell) {
        status = cell.status;
        total = cell.total;
        current = cell.current;
    }
 
    public void set(int _status, int _total, int _current) {
        status = _status;
        total = _total;
        current = _current;
    }
 
    @Override
    public String toString() {
        return "Cell [status=" + status + ", total=" + total + ", current=" + current + "]";
    }
 
}
cs

'알고리즘' 카테고리의 다른 글

핀볼 게임 [swexpert academy]  (0) 2019.10.07
벽돌 깨기[swexpert academy]  (0) 2019.10.02
보물상자 비밀번호[swexpert academy]  (0) 2019.10.01
활주로건설[swexpert academy]  (0) 2019.10.01
홈방범서비스[swexpertacademy]  (0) 2019.10.01