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++
- 재귀
- Team Fortress 2
- 백준
- 시뮬레이션
- 트리
- 알고리즘
- 브루트포스
- Unreal Engine 5
- ue5
- 유니티
- 백트래킹
- 문자열
- DFS
- BFS
- 구현
- 그리디 알고리즘
- XR Interaction Toolkit
- 다익스트라
- 다이나믹 프로그래밍
- 누적 합
- 정렬
- 자료구조
- 그래프
- 수학
- 유니온 파인드
- 투 포인터
- 스택
- 우선순위 큐
- VR
Archives
- Today
- Total
1일1알
백준 13549번 숨바꼭질 3 C++ 본문
bfs로 풀었는데, 위치를 찾았는지를 저장하는 found배열을 찾았는지 여부와, 얼마만에 찾았는지 시간의 정보를 함께 저장해서 찾았다고 무조건 건너뛰는 것이 아니라 얼마만에 찾았는지 비교하여서 만약 지금 큐에 넣으려는 경로가 더 빠르다면 이미 찾은 위치라고 하더라도 큐에 넣어주었다.
#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 n, k;
vector<pair<bool, int>> found(100001, { false,0 });
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int ans = 987654321;
cin >> n >> k;
queue<pair<int, int>> q;
q.push({ n,0 });
found[n] = { true,0 };
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.first == k) {
ans = min(ans, curr.second);
continue;
}
int next;
next = curr.first - 1;
if (next >= 0) {
if (found[next].first) {
if (found[next].second > curr.second + 1) {
q.push({ next,curr.second + 1 });
found[next] = { true,curr.second + 1 };
}
}
else {
q.push({ next,curr.second + 1 });
found[next] = { true,curr.second + 1 };
}
}
next = curr.first + 1;
if (next <= 100000) {
if (found[next].first) {
if (found[next].second > curr.second + 1) {
q.push({ next,curr.second + 1 });
found[next] = { true,curr.second + 1 };
}
}
else {
q.push({ next,curr.second + 1 });
found[next] = { true,curr.second + 1 };
}
}
next = curr.first * 2;
if (next == 0) continue;
if (next > 100000) continue;
if (found[next].first) {
if (found[next].second <= curr.second) continue;
}
q.push({ next,curr.second});
found[next] = { true,curr.second};
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1068번 트리 C++ (0) | 2022.01.15 |
---|---|
백준 5014번 스타트링크 C++ (0) | 2022.01.14 |
백준 9019번 DSLR C++ (0) | 2022.01.12 |
백준 14939번 불 끄기 C++ (0) | 2022.01.11 |
백준 18809번 Gaaaaaaaaaarden C++ (0) | 2022.01.10 |