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
- 백트래킹
- VR
- BFS
- 그리디 알고리즘
- 수학
- c++
- 시뮬레이션
- Unreal Engine 5
- 스택
- XR Interaction Toolkit
- 트리
- 구현
- 자료구조
- 누적 합
- 백준
- 문자열
- 그래프
- Team Fortress 2
- 유니티
- 알고리즘
- 투 포인터
- 다이나믹 프로그래밍
- ue5
- 우선순위 큐
- 정렬
- 다익스트라
- 유니온 파인드
- 재귀
- 브루트포스
- DFS
Archives
- Today
- Total
1일1알
백준 12851번 숨바꼭질2 C++ 본문
bfs로 풀 수 있는 문제인데 특이한 점은 가장 빠른 시간만 구하는 게 아니라 그 방법이 몇 가지인지까지 구해야 한다.
최초 도달했을 때 시간을 구하고 그 시간과 같은 시간에 도착 할때마다 Count를 증가시켜주면 된다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
vector<bool> visited(100001, false);
queue<pair<int, int>> q;
q.push({ n,0 });
visited[n] = true;
int min = 987654321;
int cnt = 0;
while (!q.empty()) {
auto a = q.front();
if (a.second > min) {
q.pop();
continue;
}
visited[a.first] = true;
q.pop();
if (cnt == 0 && a.first == k) {
min = a.second;
cnt++;
}
else if (cnt != 0 && a.first == k && min == a.second) {
cnt++;
}
if (a.first + 1 <= 100000) {
if (!visited[a.first + 1]) {
q.push({ a.first + 1,a.second + 1 });
}
}
if (a.first - 1 >= 0) {
if (!visited[a.first - 1]) {
q.push({ a.first - 1, a.second + 1 });
}
}
if (a.first * 2 <= 100000) {
if (!visited[a.first * 2]) {
q.push({ a.first * 2,a.second + 1 });
}
}
}
cout << min << "\n" << cnt;
};
'알고리즘' 카테고리의 다른 글
백준 17070번 파이프 옮기기 1 C++ (0) | 2021.10.22 |
---|---|
백준 1051번 숫자 정사각형 C++ (0) | 2021.10.21 |
백준 16953번 A->B (C++) (0) | 2021.10.19 |
백준 11660번 구간 합 구하기 5 C++ (0) | 2021.10.18 |
백준 11725번 트리의 부모 찾기 C++ (0) | 2021.10.17 |