본문 바로가기

Computer Science/Algorithm

삼성 S/W expert 2382번 미생물 격리

 2382번 미생물 격리 문제


- 2차원 배열인 map을 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
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
199
200
201
202
203
204
205
206
207
208
209
210
 #include <iostream>
    #include <cstring>
    using namespace std;
    int N, M, K;
 
    typedef struct XY {
        int num;
        int max;
        int dir; //1 상 2 하 3 좌 4 우
    };
    XY map[101][101];
    XY map2[101][101];
 
    int ans = 0;
 
    void init() {
        memset(map, 0sizeof(map));
        memset(map2, 0sizeof(map2));
        ans = 0;
    }
    void one(int x, int y, int index) {
        int nx = x - 1, ny = y;
        if (index % 2 == 0) {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map2[nx][ny].num = map[x][y].num / 2;
                map2[nx][ny].dir = 2;
                return;
            }
 
            map2[nx][ny].num += map[x][y].num;
 
            if (map2[nx][ny].max < map[x][y].num) {
                map2[nx][ny].max = map[x][y].num;
                map2[nx][ny].dir = map[x][y].dir;
            }
        }
        else {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map[nx][ny].num = map2[x][y].num / 2;
                map[nx][ny].dir = 2;
 
                return;
            }
 
            map[nx][ny].num += map2[x][y].num;
 
            if (map[nx][ny].max < map2[x][y].num) {
                map[nx][ny].max = map2[x][y].num;
                map[nx][ny].dir = map2[x][y].dir;
            }
        }
    }
 
    void two(int x, int y, int index) {
        int nx = x + 1, ny = y;
        if (index % 2 == 0) {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map2[nx][ny].num = map[x][y].num / 2;
                map2[nx][ny].dir = 1;
                return;
            }
 
            map2[nx][ny].num += map[x][y].num;
 
            if (map2[nx][ny].max < map[x][y].num) {
                map2[nx][ny].max = map[x][y].num;
                map2[nx][ny].dir = map[x][y].dir;
            }
        }
        else {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map[nx][ny].num = map2[x][y].num / 2;
                map[nx][ny].dir = 1;
                return;
            }
 
            map[nx][ny].num += map2[x][y].num;
 
            if (map[nx][ny].max < map2[x][y].num) {
                map[nx][ny].max = map2[x][y].num;
                map[nx][ny].dir = map2[x][y].dir;
            }
        }
    }
    void three(int x, int y, int index) {
        int nx = x, ny = y-1;
        if (index % 2 == 0) {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map2[nx][ny].num = map[x][y].num / 2;
                map2[nx][ny].dir = 4;
 
                return;
            }
 
            map2[nx][ny].num += map[x][y].num;
 
            if (map2[nx][ny].max < map[x][y].num) {
                map2[nx][ny].max = map[x][y].num;
                map2[nx][ny].dir = map[x][y].dir;
            }
        }
        else {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map[nx][ny].num = map2[x][y].num / 2;
                map[nx][ny].dir = 4;
 
                return;
            }
 
            map[nx][ny].num += map2[x][y].num;
 
            if (map[nx][ny].max < map2[x][y].num) {
                map[nx][ny].max = map2[x][y].num;
                map[nx][ny].dir = map2[x][y].dir;
            }
        }
    }
    void four(int x, int y, int index) {
        int nx = x, ny = y+1;
        if (index % 2 == 0) {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map2[nx][ny].num = map[x][y].num / 2;
                map2[nx][ny].dir = 3;
 
                return;
            }
 
            map2[nx][ny].num += map[x][y].num;
 
            if (map2[nx][ny].max < map[x][y].num) {
                map2[nx][ny].max = map[x][y].num;
                map2[nx][ny].dir = map[x][y].dir;
            }
        }
        else {
            if (nx == N - 1 || nx < 1 || ny == N - 1 || ny < 1) {
                map[nx][ny].num = map2[x][y].num / 2;
                map[nx][ny].dir = 3;
 
                return;
            }
 
            map[nx][ny].num += map2[x][y].num;
 
            if (map[nx][ny].max < map2[x][y].num) {
                map[nx][ny].max = map2[x][y].num;
                map[nx][ny].dir = map2[x][y].dir;
            }
        }
    }
 
    void solve(int cnt, int index) {
        if (cnt == M) {
            return;
        }
 
        if (index % 2 == 0) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    if (map[i][j].num > 0) {
                        if (map[i][j].dir == 1) { one(i, j, index); }
                        if (map[i][j].dir == 2) { two(i, j, index); }
                        if (map[i][j].dir == 3) { three(i, j, index); }
                        if (map[i][j].dir == 4) { four(i, j, index); }
                    }
                }
            }
            memset(map, 0sizeof(map));
        }
        else {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    if (map2[i][j].num > 0) {
                        if (map2[i][j].dir == 1) { one(i, j, index); }
                        if (map2[i][j].dir == 2) { two(i, j, index); }
                        if (map2[i][j].dir == 3) { three(i, j, index); }
                        if (map2[i][j].dir == 4) { four(i, j, index); }
                    }
                }
            }
            memset(map2, 0sizeof(map2));
        }
    
        solve(cnt + 1, index + 1);
    }
 
    int main() {
        int T;
        cin >> T;
        for (int i = 1; i <= T; i++) {
            init();
            cin >> N >> M >> K;
            for (int i = 0; i < K; i++) {
                int x, y;
                cin >> x >> y;
                cin >> map[x][y].num >> map[x][y].dir;
            }
            solve(00);
            for (int a = 0; a < N; a++) {
                for (int b = 0; b < N; b++) {
                    ans += map[a][b].num;
                    ans += map2[a][b].num;
                }
            }
 
            cout << "#"<< i<<' ' << ans << endl;
        }
        return 0;
    }
C
cs





새로 가져온 코드.... 훨씬 더 깔끔하다.


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
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
#include<queue>
#include<functional>
#include<math.h>
#include<map>
#include<cstring>
#define inf 100000000
using namespace std;
 
 
typedef struct {
    int num, dir, big;
}node;
 
int dr[6= { 0,-1,1,0,0 };
int dc[6= { 0,0,0,-1,1 };
int suff[6= { 0,2,1,4,3 };
node arr[1005][105][105];
int main(void) {
    int tc;
    scanf("%d"&tc);
    int n, m, k;
    for (int t = 1; t <= tc; t++) {
        scanf("%d %d %d"&n, &m, &k);
        vector<node> v;
        memset(arr, 0sizeof(arr));
        for (int i = 0; i < k; i++) {
            int a, b, c, d;
            scanf("%d %d %d %d"&a, &b, &c, &d);
            arr[0][a][b] = { c,d };
        }
        for (int k = 0; k < m; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    int dir = arr[k][i][j].dir;
                    int I = i + dr[dir], J = j + dc[dir];
                    if (I == 0 || I == (n - 1|| J == 0 || J == (n - 1))
                        arr[k][i][j].num /= 2, dir = suff[dir];
                    if (arr[k][i][j].num > 0) {
                        if (arr[k + 1][I][J].big <arr[k][i][j].num) {
                            arr[k + 1][I][J].big = arr[k][i][j].num;
                            arr[k + 1][I][J].dir = dir;
                        }
                        arr[k + 1][I][J].num += arr[k][i][j].num;
                    }
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ans += arr[m][i][j].num;
            }
        }
 
        printf("#%d %d\n", t, ans);
    }
}
cs


반응형

'Computer Science > Algorithm' 카테고리의 다른 글

[BOJ 14500] 테트로미노  (0) 2018.08.06
[BOJ 1107] 리모컨  (0) 2018.08.06
[BOJ 1476] 날짜 계산  (0) 2018.08.06
삼성 S/W expert 2115번 벌꿀 채취  (0) 2018.06.07
삼성 S/W expert 2117번 홈 방범 서비스  (0) 2018.06.05