[백준] 5014 스타트링크

고재욱·2021년 10월 11일

Baekjoon

목록 보기
31/35

❓ 문제 ❓
스타트링크

💯 문제 풀이 💯
BFS 방식으로 문제를 접근했다
현재 층에서 u,d 버튼을 눌러 해당 층을 들린적이 없으면 큐에 넣어 타겟 층까지
몇번 걸리는지 확인한다.
모든 경우의 수를 확인하고 답이 안나왔을 경우 use the stairs를 표시해준다.

#include <iostream>
#include <queue>
using namespace std;
bool stairs[1000001];
int main() {
	int f, cur, target, u, d;	
	cin >> f >> cur >> target >> u >> d;
	queue<pair<int,int>> q;
	q.push({ cur, 0 });
	stairs[cur] = true;
	while (!q.empty()) {
		int floor = q.front().first;
		int cnt = q.front().second;
		q.pop();
		if (floor == target) {
			cout << cnt;
			return 0;
		}
		if ((floor + u <= f) && !stairs[floor + u]) {
			stairs[floor + u] = true;
			q.push({ floor + u, cnt + 1 });
		}
		if ((floor - d >= 1) && !stairs[floor - d]) {
			stairs[floor - d] = true;
			q.push({ floor - d, cnt + 1 });
		}
	}
	cout << "use the stairs";
}

0개의 댓글