-

[삼성SW테스트] 백준 17144번 - 미세먼지 안녕! 본문

1-1. 삼성 SW 테스트

[삼성SW테스트] 백준 17144번 - 미세먼지 안녕!

asdklfjlasdlfkj 2019. 12. 30. 11:10

https://www.acmicpc.net/problem/17144

 

17144번: 미세먼지 안녕!

미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사과는 뛰어난 코딩 실력을 이용해 각 칸 (r, c)에 있는 미세먼지의 양을 실시간으로 모니터링하는 시스템을 개발했다. (r, c)는 r행 c열을 의미한다. 공기청정기는 항상 왼쪽 열에 설치되어 있고, 크기는 두 행을 차지한다. 공기청정기가 설치되어 있지 않은 칸에는 미세먼

www.acmicpc.net

재미있는 시뮬레이션 문제였다.

 

유의할 점은

1. 미세먼지들이 동시에 확산되므로 칸 마다 더해야 할 사방의 요소들을 3차원 벡터에 저장하고 각 칸에서 남아있는 먼지에 해당 칸의 더해줘야할 요소들을 모두 더해줘야 한다는 것,

2. 청소기에서 처음에 나오는 칸은 먼지의 양을 0으로 해줘야 하는 처리가 필요한 점 

정도였다.

 

처음에 구상한대로 답이 나오지 않아 무엇이 문제인가 하고 1~2분 코드를 다시 보니 벡터 초기화를 까먹어 틀린답을 내놨다. 연산에 필요한 벡터는 해당 연산 후 clear()를 제대로 해주는걸 잊지말자! 한번에 성공떠서 기분 좋음 :)

 

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
#include <iostream>
#include <vector>
#define maxlen 51
 
using namespace std;
 
int R, C, T;
int Map[maxlen][maxlen];
vector<int> Plus[maxlen][maxlen]; // 얼마나 더해야 하는건지
int temp[maxlen][maxlen];
 
typedef struct dir{
    int dr, dc;
}dir;
dir direction[4= { { 01 }, { 0-1 }, { 10 }, { -10 } }; //  E W S N
int Robot_Up_R, Robot_Down_R;
int Robot_C = 1;
bool Up_Exist = false;
 
void Copy2Temp(){
    for (int r = 1; r <= R; r++){
        for (int c = 1; c <= C; c++){
            temp[r][c] = Map[r][c];
        }
    }
}
 
void Spread(){
    // 미세먼지 확산
    for (int r = 1; r <= R; r++){
        for (int c = 1; c <= C; c++){
            if (Map[r][c] != -1 && Map[r][c] != 0){
                // 먼지 있다면 퍼지자
                int spread_cnt = 0;
                for (int i = 0; i<4; i++){
                    int n_r = r + direction[i].dr;
                    int n_c = c + direction[i].dc;
                    if (n_r >= 1 && n_r <= R && n_c >= 1 && n_c <= C){
                        if (Map[n_r][n_c] != -1){
                            spread_cnt++;
                            Plus[n_r][n_c].push_back(Map[r][c] / 5);
                        }
                    }
                }
                Map[r][c] -= (Map[r][c]/5* spread_cnt;
            }
        }
    }
 
    for (int r = 1; r <= R; r++){
        for (int c = 1; c <= C; c++){
            for (int plus = 0; plus < Plus[r][c].size(); plus++){
                Map[r][c] += Plus[r][c][plus];
            }
            Plus[r][c].clear();
        }
    }
}
 
void Circulation(){
    // 공기 청정기 가동
    Copy2Temp();
 
    // 위쪽 부분부터
    // Map과 temp를 활용
    Map[Robot_Up_R][Robot_C + 1= 0;
    for (int new_c = 3; new_c <= C; new_c++){
        Map[Robot_Up_R][new_c] = temp[Robot_Up_R][new_c - 1];
    }
    for (int new_r = Robot_Up_R - 1; new_r >= 1; new_r--){
        Map[new_r][C] = temp[new_r + 1][C];
    }
    for (int new_c = C - 1; new_c >= 1; new_c--){
        Map[1][new_c] = temp[1][new_c + 1];
    }
    for (int new_r = 2; new_r < Robot_Up_R; new_r++){
        Map[new_r][1= temp[new_r - 1][1];
    }
 
    // 아랫쪽 부분 순환
    Map[Robot_Down_R][Robot_C + 1= 0;
    for (int new_c = 3; new_c <= C; new_c++){
        Map[Robot_Down_R][new_c] = temp[Robot_Down_R][new_c - 1];
    }
    for (int new_r = Robot_Down_R + 1; new_r <= R; new_r++){
        Map[new_r][C] = temp[new_r - 1][C];
    }
    for (int new_c = C - 1; new_c >= 1; new_c--){
        Map[R][new_c] = temp[R][new_c + 1];
    }
    for (int new_r = R - 1; new_r > Robot_Down_R; new_r--){
        Map[new_r][1= temp[new_r + 1][1];
    }
}
 
int DustAnswer(){
    int dust = 0;
    for (int r = 1; r <= R; r++){
        for (int c = 1; c <= C; c++){
            if (Map[r][c] != -1)
                dust += Map[r][c];
        }
    }
    return dust;
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> R >> C >> T;
    for (int r = 1; r <= R; r++){
        for (int c = 1; c <= C; c++){
            cin >> Map[r][c]; // 0 : 청정구역, -1: 로봇, 기타숫자: 미세먼지 양.
            if (Map[r][c] == -1 && !Up_Exist){
                Up_Exist = true;
                Robot_Up_R = r;
            }
            else if (Map[r][c] == -1 && Up_Exist){
                Robot_Down_R = r;
            }
        }
    }
    int time = 0;
    while (time < T){
        time++;
        Spread();
        Circulation();
    }
    cout << DustAnswer() << '\n';
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
 

Comments