간단한 구현 문제였으며 특별한 예외 경우를 생각할 건 없었다.

https://github.com/ukjinlee66/BOJ/blob/master/17144.cpp

 

ukjinlee66/BOJ

baekjoon. Contribute to ukjinlee66/BOJ development by creating an account on GitHub.

github.com

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

 

17144번: 미세먼지 안녕!

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

www.acmicpc.net


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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
typedef pair<intint> P;
int R, C, T, time;
P up, down;//공기청정기 위,아래 부분.
int map[51][51];
int copymap[51][51];
//미세먼지 확장->공기청정기작동 ->시간++
bool check(int x, int y) //범위구간체크.
{
    return (x < 0 || y < 0 || x >= R || y >= C);
}
void init()
{
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            if (copymap[i][j] != -1)
                copymap[i][j] = 0;
}
void mapcopy2()
{
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            map[i][j] = copymap[i][j];
}
int check2(int x, int y) //인접한 0의개수체크.
{
    int cnt = 0;
    if (map[x + 1][y] != -1 && !check(x + 1, y))
        cnt++;
    if (map[x][y + 1!= -1 && !check(x, y + 1))
        cnt++;
    if (map[x - 1][y] != -1 && !check(x - 1, y))
        cnt++;
    if (map[x][y - 1!= -1 && !check(x, y - 1))
        cnt++;
    return cnt;
}
void print()
{
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
            printf("%d ", map[i][j]);
        printf("\n");
    }
}
int sum()
{
    int res = 0;
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            if (map[i][j] > 0)
                res += map[i][j];
    return res;
}
void upwind(P a)
{
    int x = a.first;
    int y = a.second;
    map[x - 1][y] = 0;//빨려들어간 미세먼지.
    for (int i = x - 2; i >= 0; i--)
        map[i + 1][y] = map[i][y];
    map[0][0= 0;
    for (int i = 0; i < C; i++)
        map[0][i] = map[0][i + 1];
    map[0][C - 1= 0;
    for (int i = 0; i < x; i++)
        map[i][C - 1= map[i + 1][C - 1];
    map[x][C - 1= 0;
    for (int i = C - 1; i > 0; i--)
        map[x][i] = map[x][i - 1];
    map[x][y + 1= 0;
}
void downwind(P a)
{
    int x = a.first;
    int y = a.second;
    map[x + 1][y] = 0;//빨려들어간 미세먼지.
    for (int i = x + 2; i < R; i++)
        map[i - 1][y] = map[i][y];
    map[R - 1][y] = 0;
    for (int i = 0; i < C; i++)
        map[R - 1][i] = map[R - 1][i + 1];
    map[R - 1][C - 1= 0;
    for (int i = R - 1; i > x; i--)
        map[i][C - 1= map[i - 1][C - 1];
    map[x][C - 1= 0;
    for (int i = C - 1; i > 1; i--)
        map[x][i] = map[x][i - 1];
    map[x][y + 1= 0;
}
void bfs()
{
    queue<P> Q;
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++)
            if (map[i][j] > 0)
                Q.push({ i,j });
    while (!Q.empty())
    {
        int Siz = Q.size();
        while (Siz--)
        {
            int curx = Q.front().first;
            int cury = Q.front().second;
            Q.pop();
            int dd = check2(curx, cury);
            for (int i = 0; i < 4; i++)
            {
                int nx = curx + dx[i];
                int ny = cury + dy[i];
                if (check(nx, ny)) continue;
                if (map[nx][ny] != -1)//4방향중 칸을발견.
                {
                    Q.push({ nx,ny });
                    copymap[nx][ny] += map[curx][cury] / 5;
                }
            }
            copymap[curx][cury] += map[curx][cury] - (map[curx][cury] / 5* dd;
            //남은 미세먼지의 양.
        }
        return;
    }
}
int main()
{
    scanf("%d %d %d"&R, &C, &T);
    bool flag = false;
    for (int i = 0; i < R; i++)
    {
        for (int j = 0; j < C; j++)
        {
            scanf("%d"&map[i][j]);
            if (!flag && map[i][j] == -1)
            {
                up = make_pair(i, j);
                flag = true;
                copymap[i][j] = map[i][j];
            }
            else if (flag && map[i][j] == -1)
            {
                down = make_pair(i, j);
                copymap[i][j] = map[i][j];
            }
        }
    }
    flag = false;
    while (T--)
    {
        bfs();
        mapcopy2();
        init();
        upwind(up);
        downwind(down);
    }
    printf("%d", sum());
}
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
 
 

 

'Problem Solving > 삼성 역량테스트' 카테고리의 다른 글

BOJ-14500 테트로미노  (0) 2019.11.07
BOJ-17837 새로운 게임2  (0) 2019.11.06
BOJ-12100 2048(Easy)  (0) 2019.11.06
BOJ-17780 새로운 게임  (0) 2019.11.05
BOJ-17779 게리맨더링 2  (0) 2019.11.05

+ Recent posts