BOJ 5014: 스타트링크

백윤재·2021년 8월 30일
0

BOJ

목록 보기
8/28
post-thumbnail

✔ 문제 링크


BOJ 5014: 스타트링크


✔ 문제해결전략

  • 그래프 탐색
  • 1차원에서의 BFS(Breadth First Search)

✔ 해결과정

  • 정직하게 BFS 하면 된다.

✔ 정답 Code

#include <bits/stdc++.h>
using namespace std;

int stair[1000001]; // count of button to reach each stair

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int f, s, g, u ,d;
  queue<int> q;
  cin >> f >> s >> g >> u >> d;

  fill( stair, stair + 1000001, -1);
  stair[s] = 0;
  q.push(s);
  while(!q.empty()) {
    int cur = q.front();
    q.pop();

    for(int new_: {cur-d, cur+u}) {
      if(new_<1 || new_>f) continue;
      if(stair[new_] != -1) continue;
      stair[new_] = stair[cur] + 1;
      q.push(new_);
    }
  }
  
  if(stair[g]==-1) {
	  cout << "use the stairs";
	  return 0;
  }
  cout << stair[g] << '\n';
  
  
}
profile
SKKU 18.5

0개의 댓글