Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 트리
- 다이나믹 프로그래밍
- 그리디 알고리즘
- 유니온 파인드
- 스택
- 그래프
- 정렬
- c++
- 유니티
- Unreal Engine 5
- 알고리즘
- DFS
- 다익스트라
- VR
- 브루트포스
- 투 포인터
- 재귀
- 자료구조
- BFS
- 누적 합
- Team Fortress 2
- 백트래킹
- ue5
- XR Interaction Toolkit
- 구현
- 문자열
- 우선순위 큐
- 백준
- 시뮬레이션
- 수학
Archives
- Today
- Total
1일1알
백준 5014번 스타트링크 C++ 본문
어렵지 않은 bfs 탐색 문제이다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
int f, s, g, u, d;
vector<bool> found(1000001, false);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> f >> s >> g >> u >> d;
queue<pair<int, int>> q;
q.push({ s,0 });
found[s] = true;
int ans = -1;
bool isPossible = false;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.first == g) {
isPossible = true;
ans = curr.second;
break;
}
if (u != 0) {
int nextFloor = curr.first + u;
if (nextFloor <= f) {
if (!found[nextFloor]) {
q.push({ nextFloor,curr.second + 1 });
found[nextFloor] = true;
}
}
}
if (d != 0) {
int nextFloor = curr.first - d;
if (nextFloor > 0) {
if (!found[nextFloor]) {
q.push({ nextFloor,curr.second + 1 });
found[nextFloor] = true;
}
}
}
}
if (isPossible) {
cout << ans;
}
else {
cout << "use the stairs";
}
};
'알고리즘' 카테고리의 다른 글
백준 2210번 숫자판 점프 C++ (0) | 2022.01.16 |
---|---|
백준 1068번 트리 C++ (0) | 2022.01.15 |
백준 13549번 숨바꼭질 3 C++ (0) | 2022.01.13 |
백준 9019번 DSLR C++ (0) | 2022.01.12 |
백준 14939번 불 끄기 C++ (0) | 2022.01.11 |