#include <stdio.h> #include <iostream> #include <queue> #include <vector> #include <algorithm> using namespace std; int total, start, arrive, up, down; int visited[1000010]; queue<int> q; void BFS() { visited[start] = 1; q.push(start); while(!q.empty()) { int x = q.front(); q.pop(); for(int i = 1; i <= 2; i++) { int xpos; // up if(i == 1) { xpos = x + up; } // down else if(i == 2) { xpos = x - down; } if(xpos >= 1 && xpos <= total && visited[xpos] == 0) { visited[xpos] = visited[x] + 1; q.push(xpos); } if(xpos == arrive) { return; } } } } int main(void) { // freopen("B5014_input.txt", "r", stdin); cin >> total >> start >> arrive >> up >> down; BFS(); if(visited[arrive] == 0) { cout << "use the stairs"; } else { cout << visited[arrive]-1; } return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 2468] 안전영역 (BFS) (C/C++) (0) | 2019.12.04 |
---|---|
[백준 9177] 단어 섞기 (DFS/BFS) (C/C++) (★★) (0) | 2019.12.04 |
[백준 7562] 나이트의 이동 (BFS) (C/C++) (0) | 2019.12.03 |
[백준 2251] 물통 (BFS) (C/C++) (0) | 2019.12.02 |
[백준 1697] 숨바꼭질 (BFS) (C/C++) (0) | 2019.12.02 |