문제링크 : https://www.acmicpc.net/problem/13700
이 문제는 전형적인 BFS의 최소시간 구하기 문제이다.
빌딩 배열을 선언한 후, 경찰서는 가면 안되니 방문했다고 표시한다.
그리고 현재 금은방 S가 root 즉, 시점이므로 여기서부터 출발하여
집으로 가는데, 집 주소인 D로 못가게 되었다면 BUGFOUND를 출력하면 되는 문제이다.
소스코드 :
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
|
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int N, S, D, F, B, K;
int Next(int t, int n) {
// t == 0 => forward, t == 1 => rear.
if (t == 0) {
return n + F;
}
else return n - B;
}
int main() {
cin >> N >> S >> D >> F >> B >> K;
vector<int> building(N + 1);
int root = S;
int house = D;
for (int i = 1; i <= K; i++) {
int police;
cin >> police;
building[police] = 1;
}
int pushBtn = 0;
bool isCleared = false;
queue<int> q;
q.push(root);
while(!q.empty()) {
int qSize = q.size();
for (int i = 0; i < qSize; i++) {
int currentBuilding = q.front();
if (currentBuilding == house) {
// 정답
cout << pushBtn << endl;
while (!q.empty()) q.pop();
isCleared = true;
break;
}
q.pop();
for (int t = 0; t < 2; t++) {
int nextBuilding = Next(t, currentBuilding);
if (nextBuilding >= 1 && nextBuilding <= N + 1 && building[nextBuilding] == 0) {
q.push(nextBuilding);
building[nextBuilding] = 1;
}
}
}
pushBtn++;
}
if (!isCleared) {
cout << "BUG FOUND" << endl;
}
}
| cs |
댓글
댓글 쓰기