https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpLlKAQ4DFAUq

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

BFS를 이용하고 터널 구조물 타입에 따라서 하나하나 조건을 처리해줬다.

 

 

예를 들어 1번 터널 구조물의 경우에는

위쪽 방향에 있으면 이동할 수 있는 터널은 1, 2, 5, 6번

아래쪽 방향에 있으면 이동할 수 있는 터널은 1, 2, 4, 7번

왼쪽은 1, 3, 4, 5번, 오른쪽은 1, 3, 6, 7번이다.

 

이런 식으로 각 터널 구조물마다 이동할 수 있는 각 방향에 올 수 있는 터널이 있을 때 큐에 넣어서 이동할 수 있도록해줬다. 다른 사람들의 풀이를 보면 훨씬 깔끔하게 구현하신 분들이 많긴 하다... 나중에 다시 찾아보고 공부해봐야겠다.

 

 

BFS는 입력 받은 l시간만큼만 진행하기 위해 큐 사이즈만큼씩 진행했다.

(큐 사이즈만큼 이 각 단계(시간) 이므로)

 

 

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
258
259
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
 
int ans;
int n, m, r, c, l;
int map[50][50];
bool check[50][50];
 
int dx[] = { -1010 };
int dy[] = { 010-1 };
 
void solve() {
    int time = 1;
    queue<pair<intint>> q;
    q.push(make_pair(r, c));
    check[r][c] = true;
 
    int qsize;
    int cnt = 1;
    while (!q.empty()) {
        qsize = q.size();
        if (time == l) break;
        time++;
 
        while (qsize-- > 0) {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
 
            int nx, ny;
            if (map[x][y] == 1) {
 
                for (int k = 0; k < 4; k++) {
                    nx = x + dx[k];
                    ny = y + dy[k];
                    if (nx < 0 || ny < 0 || nx >= n || ny >m) continue;
                    if (check[nx][ny]) continue;
                    if (map[nx][ny] == 0continue;
 
                    if (k == 0) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 5 || map[nx][ny] == 6) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else if (k == 1) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 6 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else if (k == 2) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 4 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 4 || map[nx][ny] == 5) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
 
            }
            else if (map[x][y] == 2) {
                nx = x + dx[0];
                ny = y + dy[0];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
                    if (!check[nx][ny]) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 5 || map[nx][ny] == 6) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
 
                nx = x + dx[2];
                ny = y + dy[2];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
                    if (!check[nx][ny]) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 4 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
 
 
            }
            else if (map[x][y] == 3) {
                nx = x + dx[1];
                ny = y + dy[1];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
                    if (!check[nx][ny]) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 6 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
 
                    }
                }
 
                nx = x + dx[3];
                ny = y + dy[3];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m) {
                    if (!check[nx][ny]) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 4 || map[nx][ny] == 5) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
 
                    }
                }
            }
            else if (map[x][y] == 4)
            {
                for (int k = 0; k < 2; k++) {
                    nx = x + dx[k];
                    ny = y + dy[k];
                    if (nx < 0 || ny < 0 || nx >= n || ny >m) continue;
                    if (check[nx][ny]) continue;
                    if (map[nx][ny] == 0continue;
 
                    if (k == 0) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 5 || map[nx][ny] == 6) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else if (k == 1) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 6 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
 
 
            }
            else if (map[x][y] == 5) {
                for (int k = 1; k < 3; k++) {
                    nx = x + dx[k];
                    ny = y + dy[k];
                    if (nx < 0 || ny < 0 || nx >= n || ny >m) continue;
                    if (check[nx][ny]) continue;
                    if (map[nx][ny] == 0continue;
 
                    if (k == 1) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 6 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else if (k == 2) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 4 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
            }
            else if (map[x][y] == 6) {
                for (int k = 2; k < 4; k++) {
                    nx = x + dx[k];
                    ny = y + dy[k];
                    if (nx < 0 || ny < 0 || nx >= n || ny >m) continue;
                    if (check[nx][ny]) continue;
                    if (map[nx][ny] == 0continue;
 
                    if (k == 2) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 4 || map[nx][ny] == 7) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 4 || map[nx][ny] == 5) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
            }
            else if (map[x][y] == 7) {
                for (int k = 0; k < 4; k += 3) {
                    nx = x + dx[k];
                    ny = y + dy[k];
                    if (nx < 0 || ny < 0 || nx >= n || ny >m) continue;
                    if (check[nx][ny]) continue;
                    if (map[nx][ny] == 0continue;
 
 
                    if (k == 0) {
                        if (map[nx][ny] == 1 || map[nx][ny] == 2 || map[nx][ny] == 5 || map[nx][ny] == 6) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                    else {
                        if (map[nx][ny] == 1 || map[nx][ny] == 3 || map[nx][ny] == 4 || map[nx][ny] == 5) {
                            q.push(make_pair(nx, ny));
                            check[nx][ny] = true;
                            cnt++;
                        }
                    }
                }
            }
 
        }
    }
 
 
    ans = cnt;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie();
 
    int T;
    cin >> T;
    for (int tc = 1; tc <= T; tc++) {
        ans = 0;
        memset(check, falsesizeof(check));
        cin >> n >> m >> r >> c >> l;
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> map[i][j];
            }
        }
 
        solve();
 
        cout << '#' << tc << ' ' << ans << "\n";
 
    }
 
    return 0;
}
Colored by Color Scripter
 

+ Recent posts