승쨩개발공부
[BJ] 1697 - 숨바꼭질 (BPS 1차원 최단거리 변형) 본문
CodingTestTraining/BaekJoon(Basic)
[BJ] 1697 - 숨바꼭질 (BPS 1차원 최단거리 변형)
SeungHyune 2025. 10. 28. 12:10#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n, k;
int vis[100005];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
queue<int> q;
vis[n] = 1;
q.push(n);
while (q.size())
{
int now = q.front();
q.pop();
for (int next : {now + 1, now - 1, now * 2})
{
if (next < 0 || next > 100000) continue;
if (vis[next]) continue;
q.push(next);
vis[next] = vis[now] + 1;
}
}
cout << vis[k] - 1;
return 0;
}'CodingTestTraining > BaekJoon(Basic)' 카테고리의 다른 글
| [BJ] 2839 - 설탕배달 (그리디) (0) | 2025.10.28 |
|---|---|
| [BJ] 6603 - 로또 (백트래킹) (0) | 2025.10.28 |
| [BJ] 1260 - DFS와 BFS (그래프) (0) | 2025.10.28 |
| [BJ] 2568 - 안전 영역 (DFS 모든 경우의 수) (0) | 2025.10.28 |
| [BJ] 1629 - 곱샘 (재귀기초, 모듈러 성질, 귀납적사고) (0) | 2025.03.06 |