반응형

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

 

23289번: 온풍기 안녕!

유난히 추운 날씨가 예상되는 이번 겨울을 대비하기 위해 구사과는 온풍기를 설치하려고 한다. 온풍기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 온풍기의 위치 배열, 벽의 위치 배열과 온도를 저장할 배열을 만들어 각각의 정보를 저장합니다.

 

2. 벽의 위치 배열의 경우 x, y 좌표에서 위 오른쪽 아래 왼쪽 방향에 벽이 있다면 각각 wall [ y ][ x ][ 0 ], wall [ y ][ x ][ 1 ], wall [ y ][ x ][ 2 ], wall [ y ][ x ][ 3 ]의 값을 1로 바꿔줍니다.

 

3. bfs를 활용하여 온풍기를 작동시킵니다.

 

4. 온도를 조절하고 초콜릿을 먹습니다.

 

5. 온도를 체크하는 위치에 온도가 모두 K이상이라면 현재 먹은 초콜릿의 개수를 출력합니다.

 

6. K이상이 될 때까지 위 과정을 반복합니다.

 

7. 만약 횟수가 100회를 초과한다면 101을 출력합니다.

 

[ 소스코드 ]

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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include<iostream>
#include<queue>
 
using namespace std;
 
int R, C, K, W;
int arr[21][21];
int wall[21][21][4];    //벽의 위치 배열
int temp[21][21];
int tempT[21][21];
 
int dy[4= { -1,0,1,0 };
int dx[4= { 0,1,0,-1 };
 
int dr[5][3= {    //온풍기 방향 배열
    0,0,0,
    -1,0,1,
    -1,0,1,
    -1,-1,-1,
    1,1,1
};
int dc[5][3= {
    0,0,0,
    1,1,1,
    -1,-1,-1,
    -1,0,1,
    -1,0,1
};
 
struct pos {
    int y;
    int x;
    int temp;
};
 
bool check()    //온도를 조사해야 하는 모든 칸의 온도가 K이상인지 체크
{
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            if (arr[i][j] == 5) {
                if (temp[i][j] < K) {
                    return false;
                }
            }
        }
    }
 
    return true;
}
 
void adjust()        //온도 조절
{
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            tempT[i][j] = temp[i][j];
        }
    }
 
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            for (int k = 0; k < 4; k++) {
                int yy = i + dy[k];
                int xx = j + dx[k];
                if (yy > 0 && yy <= R && xx > 0 && xx <= C && wall[i][j][k] != 1) {
                    if (temp[i][j] < temp[yy][xx]) {
                        int dif = temp[yy][xx] - temp[i][j];
                        tempT[i][j] += dif / 4;
                    }
                    else if(temp[i][j] > temp[yy][xx]){
                        int dif = temp[i][j] - temp[yy][xx];
                        tempT[i][j] -= dif / 4;
                    }
                }
            }
        }
    }
 
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            temp[i][j] = tempT[i][j];
        }
    }
 
    for (int i = 1; i <= R; i++) {
        if (temp[i][1> 0) {
            temp[i][1]--;
        }
        if (temp[i][C] > 0) {
            temp[i][C]--;
        }
    }
 
    for (int i = 2; i < C; i++) {
        if (temp[1][i] > 0) {
            temp[1][i]--;
        }
        if (temp[R][i] > 0) {
            temp[R][i]--;
        }
    }
}
 
void bfs(int y, int x, int dir)    //온풍기 확산
{
    int visited[21][21= { 0 };
    queue<pos> q;
    q.push({ y,x,5 });
    visited[y][x] = 1;
    temp[y][x] += 5;
 
    while (!q.empty()) {
        int cury = q.front().y;
        int curx = q.front().x;
        int curt = q.front().temp;
        q.pop();
 
        if (curt == 0continue;
 
        for (int i = 0; i < 3; i++) {
            int yy = cury + dr[dir][i];
            int xx = curx + dc[dir][i];
            if (yy > 0 && yy <= R && xx > 0 && xx <= C) {
                bool flag = false;
                if (visited[yy][xx] != 1) {
                    if (dir == 1) {
                        if (wall[yy][xx][3!= 1) {
                            if (i == 0) {
                                if (wall[yy][xx - 1][2!= 1) {
                                    flag = true;
                                }
                            }
                            else if (i == 1) {
                                flag = true;
                            }
                            else {
                                if (wall[yy][xx - 1][0!= 1) {
                                    flag = true;
                                }
                            }
                        }
                    }
                    else if (dir == 2) {
                        if (wall[yy][xx][1!= 1) {
                            if (i == 0) {
                                if (wall[yy][xx + 1][2!= 1) {
                                    flag = true;
                                }
                            }
                            else if (i == 1) {
                                flag = true;
                            }
                            else {
                                if (wall[yy][xx + 1][0!= 1) {
                                    flag = true;
                                }
                            }
                        }
                    }
                    else if (dir == 3) {
                        if (wall[yy][xx][2!= 1) {
                            if (i == 0) {
                                if (wall[yy + 1][xx][1!= 1) {
                                    flag = true;
                                }
                            }
                            else if (i == 1) {
                                flag = true;
                            }
                            else {
                                if (wall[yy + 1][xx][3!= 1) {
                                    flag = true;
                                }
                            }
                        }
                    }
                    else {
                        if (wall[yy][xx][0!= 1) {
                            if (i == 0) {
                                if (wall[yy - 1][xx][1!= 1) {
                                    flag = true;
                                }
                            }
                            else if (i == 1) {
                                flag = true;
                            }
                            else {
                                if (wall[yy - 1][xx][3!= 1) {
                                    flag = true;
                                }
                            }
                        }
                    }
 
                    if (flag == true) {
                        visited[yy][xx] = 1;
                        temp[yy][xx] += curt - 1;
                        q.push({ yy,xx,curt - 1 });
                    }
                }
            }
        }
    }
}
 
int main()
{
    scanf("%d %d %d"&R, &C, &K);
 
    for (int i = 1; i <= R; i++) {
        for (int j = 1; j <= C; j++) {
            scanf("%d"&arr[i][j]);
        }
    }
 
    scanf("%d"&W);
 
    for (int i = 0; i < W; i++) {
        int x, y, t;
        scanf("%d %d %d"&y, &x, &t);
        if (t == 0) {
            wall[y][x][0= 1;
            wall[y - 1][x][2= 1;
        }
        else {
            wall[y][x][1= 1;
            wall[y][x + 1][3= 1;
        }
    }
 
    int ans = 0;
 
    while (1) {
        for (int i = 1; i <= R; i++) {
            for (int j = 1; j <= C; j++) {
                if (arr[i][j] > 0 && arr[i][j] < 5) {
                    int yy = i + dr[arr[i][j]][1];
                    int xx = j + dc[arr[i][j]][1];
                    bfs(yy, xx, arr[i][j]);
                }
            }
        }
 
        adjust();
 
        ans++;
 
        if (ans > 100) {
            printf("%d"101);
            return 0;
        }
 
        if (check()) {
            printf("%d", ans);
            return 0;
        }
    }
}
cs
반응형

+ Recent posts