반응형
이 문제는 bfs를 통해 해결하였습니다.
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
|
#include<iostream>
#include<queue>
#include<memory.h>
using namespace std;
struct info {
int x;
int y;
};
int N, M, R, C, L;
int map[51][51];
queue<info>q;
int ans;
bool visited[51][51];
void bfs() {
int time_ = 1;
while (1) {
visited[R][C] = true;
if (time_ == L) break;
int q_end = q.size();
time_++;
for (int i = 0; i < q_end; i++) {
info cur = q.front();
info next;
q.pop();
if (map[cur.y][cur.x] == 1) {
if (cur.y + 1 > -1 && cur.y + 1 < N) {
if (visited[cur.y + 1][cur.x] == false) {
if (map[cur.y + 1][cur.x] == 1 || map[cur.y + 1][cur.x] == 2 || map[cur.y + 1][cur.x] == 7 || map[cur.y + 1][cur.x] == 4) {
next.y = cur.y + 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.y - 1 > -1 && cur.y - 1 < N) {
if (visited[cur.y - 1][cur.x] == false) {
if (map[cur.y - 1][cur.x] == 1 || map[cur.y - 1][cur.x]==2|| map[cur.y - 1][cur.x] == 6|| map[cur.y - 1][cur.x] == 5) {
next.y = cur.y - 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x + 1 > -1 && cur.x + 1 < M) {
if (visited[cur.y][cur.x + 1] == false) {
if (map[cur.y][cur.x + 1] == 1 || map[cur.y][cur.x + 1]==3|| map[cur.y][cur.x + 1]==6 || map[cur.y][cur.x + 1] ==7 ) {
next.y = cur.y;
next.x = cur.x + 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x - 1 > -1 && cur.x - 1 < M) {
if (visited[cur.y][cur.x - 1] == false) {
if (map[cur.y][cur.x - 1] == 1 || map[cur.y][cur.x - 1] == 3 || map[cur.y][cur.x - 1] == 4 || map[cur.y][cur.x - 1] == 5) {
next.y = cur.y;
next.x = cur.x - 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
if (map[cur.y][cur.x] == 2) {
if (cur.y + 1 > -1 && cur.y + 1 < N) {
if (map[cur.y + 1][cur.x] == 1 || map[cur.y + 1][cur.x] == 2 || map[cur.y + 1][cur.x] == 7 || map[cur.y + 1][cur.x] == 4) {
if (visited[cur.y + 1][cur.x] == false) {
next.y = cur.y + 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.y - 1 > -1 && cur.y - 1 < N) {
if (visited[cur.y - 1][cur.x] == false) {
if (map[cur.y - 1][cur.x] == 1 || map[cur.y - 1][cur.x] == 2 || map[cur.y - 1][cur.x] == 6 || map[cur.y - 1][cur.x] == 5) {
next.y = cur.y - 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
if (map[cur.y][cur.x] == 3) {
if (cur.x + 1 > -1 && cur.x + 1 < M) {
if (visited[cur.y][cur.x + 1] == false) {
if (map[cur.y][cur.x + 1] == 1 || map[cur.y][cur.x + 1] == 3 || map[cur.y][cur.x + 1] == 6 || map[cur.y][cur.x + 1] == 7) {
next.y = cur.y;
next.x = cur.x + 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x - 1 > -1 && cur.x - 1 < M) {
if (visited[cur.y][cur.x - 1] == false) {
if (map[cur.y][cur.x - 1] == 1 || map[cur.y][cur.x - 1] == 3 || map[cur.y][cur.x - 1] == 4 || map[cur.y][cur.x - 1] == 5) {
next.y = cur.y;
next.x = cur.x - 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
if (map[cur.y][cur.x] == 4) {
if (cur.y - 1 > -1 && cur.y - 1 < N) {
if (visited[cur.y - 1][cur.x] == false) {
if (map[cur.y - 1][cur.x] == 1 || map[cur.y - 1][cur.x] == 2 || map[cur.y - 1][cur.x] == 5 || map[cur.y - 1][cur.x] == 6) {
next.y = cur.y - 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x + 1 > -1 && cur.x + 1 < M) {
if (visited[cur.y][cur.x + 1] == false) {
if (map[cur.y][cur.x + 1] == 1 || map[cur.y][cur.x + 1] == 3 || map[cur.y][cur.x + 1] == 6 || map[cur.y][cur.x + 1] == 7) {
next.y = cur.y;
next.x = cur.x + 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
if (map[cur.y][cur.x] == 5) {
if (cur.y + 1 > -1 && cur.y + 1 < N) {
if (visited[cur.y + 1][cur.x] == false) {
if (map[cur.y + 1][cur.x] == 1 || map[cur.y + 1][cur.x] == 2 || map[cur.y + 1][cur.x] == 7 || map[cur.y + 1][cur.x] == 4) {
next.y = cur.y + 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x + 1 > -1 && cur.x + 1 < M) {
if (visited[cur.y][cur.x + 1] == false) {
if (map[cur.y][cur.x + 1] == 1 || map[cur.y][cur.x + 1] == 3 || map[cur.y][cur.x + 1] == 6 || map[cur.y][cur.x + 1] == 7) {
next.y = cur.y;
next.x = cur.x + 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
if (map[cur.y][cur.x] == 6) {
if (cur.y + 1 > -1 && cur.y + 1 < N) {
if (visited[cur.y + 1][cur.x] == false) {
if (map[cur.y + 1][cur.x] == 1 || map[cur.y + 1][cur.x] == 2 || map[cur.y + 1][cur.x] == 7 || map[cur.y + 1][cur.x] == 4) {
next.y = cur.y + 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x - 1 > -1 && cur.x - 1 < M) {
if (visited[cur.y][cur.x - 1] == false) {
if (map[cur.y][cur.x - 1] == 1 || map[cur.y][cur.x - 1] == 3 || map[cur.y][cur.x - 1] == 4 || map[cur.y][cur.x - 1] == 5) {
next.y = cur.y;
next.x = cur.x - 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
if (map[cur.y][cur.x] == 7) {
if (cur.y - 1 > -1 && cur.y - 1 < N) {
if (visited[cur.y - 1][cur.x] == false) {
if (map[cur.y - 1][cur.x] == 1 || map[cur.y - 1][cur.x] == 2 || map[cur.y - 1][cur.x] == 6 || map[cur.y - 1][cur.x] == 5) {
next.y = cur.y - 1;
next.x = cur.x;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
if (cur.x - 1 > -1 && cur.x - 1 < M) {
if (visited[cur.y][cur.x - 1] == false) {
if (map[cur.y][cur.x - 1] == 1 || map[cur.y][cur.x - 1] == 3 || map[cur.y][cur.x - 1] == 4 || map[cur.y][cur.x - 1] == 5) {
next.y = cur.y;
next.x = cur.x - 1;
visited[next.y][next.x] = true;
q.push(next);
ans++;
}
}
}
}
}//큐의 크기만큼
}//while
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
scanf("%d", &T);
//cin >> T;
for (int t = 1; t <= T; t++) {
while (!q.empty()) q.pop();
ans = 1; memset(map, 0, sizeof(map));
memset(visited, false, sizeof(visited));
scanf("%d%d%d%d%d", &N, &M, &R, &C, &L);
//cin >> N >> M >> R >> C;
for (int Y = 0; Y < N; Y++) {
for (int X = 0; X < M; X++) {
scanf("%d", &map[Y][X]);
//cin >> map[Y][X];
if (Y == R && X == C) {
info st;
st.x = X;
st.y = Y;
q.push(st);
}//if
}//X
}//Y;
bfs();
printf("#%d %d\n", t, ans);
}//T
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
반응형
'알고리즘 문제풀기 > SWEA' 카테고리의 다른 글
[소프트웨어 익스퍼트 아카데미][SWEA][모의 SW 역량테스트][ C++][4008번] 숫자 만들기 (0) | 2019.08.04 |
---|---|
[소프트웨어 익스퍼트 아카데미][SWEA][모의 SW 역량테스트][ C++][4012번] 요리사 (0) | 2019.08.02 |
[소프트웨어 익스퍼트 아카데미][SWEA][모의 SW 역량테스트][ C++][4013번] 특이한 자석 (0) | 2019.08.02 |
[소프트웨어 익스퍼트 아카데미][SWEA][모의 SW 역량테스트][ C++][1952번]수영장 (0) | 2019.07.20 |
[소프트웨어 익스퍼트 아카데미][SWEA][모의 SW 역량테스트][ C++][1949번]등산로 조성 (0) | 2019.07.17 |