본문 바로가기
백준 알고리즘/BFS

백준 5014번 C++

by paysmile 2019. 8. 12.

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;
const int MAX = 1000001;
int f, s, g, u, d;
int floors[MAX];

int bfs() {
	queue<int> q;
	int answer = -1;

	q.push(s);
	floors[s] = 0;

	while (!q.empty()) {
		int current = q.front();
		if (current == g) {
			answer = floors[current];
			break;
		}
		q.pop();

		if (current + u >= 1 && current + u <= f && floors[current + u] == -1) {
			q.push(current + u);
			floors[current + u] = floors[current] + 1;
		}

		if (current - d >= 1 && current - d <= f && floors[current - d] == -1) {
			q.push(current - d);
			floors[current - d] = floors[current] + 1;
		}
	}
	return answer;
}

int main(void) {
	int answer = 0;

	cin >> f >> s >> g >> u >> d;
	
	memset(floors, -1, sizeof(floors));
	answer = bfs();
	if (answer == -1)
		cout << "use the stairs" << endl;
	else
		cout << answer << endl;
	return 0;
}

'백준 알고리즘 > BFS' 카테고리의 다른 글

백준 3055번 C++  (0) 2019.08.14
백준 1707번 C++  (0) 2019.08.14
백준 2589번 C++  (0) 2019.08.12
백준 7569번 C++  (0) 2019.08.10
백준 2468번 C++  (0) 2019.08.10