[백준] 5014번: 스타트링크

2021. 2. 5. 18:33Algorithm/백준

 

5014번: 스타트링크

첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.

www.acmicpc.net

 

#include<iostream>
#include<queue>
#define MAX 1000001
using namespace std;

int f, s, g, u, d;
int stair[MAX];
int visit[MAX];

int solution(int s) {

	int dx[2] = { u,-d };
	queue<int>q, cnt;
	q.push(s); cnt.push(0);

	visit[s] = 1;

	while (!q.empty()) {
		int x = q.front();q.pop();
		int c = cnt.front();cnt.pop();

		if (x == g) return c;

		for (int i = 0;i < 2;i++) {
			int nx = x + dx[i];
		
			if (x<1 || x>f) continue;
			if (visit[nx]) continue;

			visit[nx] = true;

			q.push(nx); cnt.push(c + 1);
		}
	}
	return -1;
}

int main() {
	cin >> f >> s >> g >> u >> d;
	int ans=solution(s);
	if (ans == -1) 
		cout << "use the stairs";
	else cout << ans;
}

'Algorithm > 백준' 카테고리의 다른 글

[백준] 10815번: 숫자 카드  (0) 2021.02.06
[백준] 2309번: 일곱 난쟁이  (0) 2021.02.05
[백준] 1926번: 그림  (0) 2021.02.05
[백준] 4949번: 균형잡힌 세상  (0) 2021.02.05
[백준] 10773번: 제로  (0) 2021.02.05