반응형
<코드>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 100000
int N, K, ans, cnt;
bool check[100001];
queue<pair<int, int>> q; // <위치, 시간>
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
q.push({ N,0 });
while (!q.empty())
{
int now = q.front().first;
int time = q.front().second;
q.pop();
check[now] = true;
// 이미 최소시간으로 방문한 경우
if (cnt && ans == time && now == K)
cnt++;
// 처음으로 방문한 경우
if (!cnt && now == K)
{
ans = time;
cnt++;
}
if (now - 1 >= 0 && !check[now - 1])
q.push({ now - 1,time + 1 });
if (now + 1 <= MAX && !check[now + 1])
q.push({ now + 1,time + 1 });
if (2 * now <= MAX && !check[2 * now])
q.push({ 2 * now,time + 1 });
}
cout << ans << '\n';
cout << cnt << '\n';
}
12851번: 숨바꼭질 2
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 때
www.acmicpc.net
반응형
'🧩PS > 🥈Nomal' 카테고리의 다른 글
[C/C++] 백준 9019번 - DSLR (0) | 2021.03.09 |
---|---|
[C/C++] 백준 13549번 - 숨바꼭질 3 (BFS) (0) | 2021.03.09 |
[C/C++] 백준 2916번 - 자와 각도기 (0) | 2021.03.07 |
[C/C++] 백준 14003번 - 가장 긴 증가하는 부분 수열 5 (LIS) (0) | 2021.03.06 |
[C/C++] 백준 12738번 - 가장 긴 증가하는 부분 수열 3 (LIS) (0) | 2021.03.04 |