티스토리 뷰
강호의 위치에서 갈 수 있는 건
1) +U 위로가기
2) -D 아래로 가기다
1~F를 제한으로 두고 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 | #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MAXN 1000010 int F, S, G, U, D; int dist[MAXN]; bool check[MAXN]; int main() { scanf(" %d %d %d %d %d", &F, &S, &G, &U, &D); queue<int> q; q.push(S); dist[S] = 0; check[S] = true; while (!q.empty()) { int here = q.front(); q.pop(); int next = here + U; if (next <= F && !check[next]) { check[next] = true; dist[next] = dist[here] + 1; q.push(next); } next = here - D; if (1 <= next && !check[next]) { check[next] = true; dist[next] = dist[here] + 1; q.push(next); } } if (check[G]) printf("%d\n", dist[G]); else puts("use the stairs"); } | cs |
'알고리즘 > BOJ' 카테고리의 다른 글
[백준] 3042 트리플렛 (0) | 2018.07.02 |
---|---|
[백준] 14528 Bovine Genomics (Silver) (0) | 2018.07.02 |
[백준] 1697 숨바꼭질 (0) | 2018.07.02 |
[백준] 2178 미로탐색 (0) | 2018.07.02 |
[백준] 2667 단지번호 붙이기 (0) | 2018.07.02 |
댓글