Recent Posts
Notice
No Rules Rules
숨바꼭질 (feat. 백준, 1697번) 본문
728x90
반응형
숨바꼭질
https://www.acmicpc.net/problem/1697
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
bool visit[100000];
int bfs(register int n, register int k) {
visit[n] = true;
queue<pair<int, int>> q;
q.push(make_pair(n, 0));
while (!q.empty()) {
auto v = q.front(); q.pop();
auto x = v.first, c = v.second;
if (x == k)
return c;
c += 1;
if (x - 1 >= 0 && !visit[x - 1])
visit[x - 1] = true, q.push(make_pair(x - 1, c));
if (x + 1 <= 100000 && !visit[x + 1])
visit[x + 1] = true, q.push(make_pair(x + 1, c));
if ((x << 1) <= 100000 && !visit[x << 1])
visit[x << 1] = true, q.push(make_pair(x << 1, c));
}
return 0;
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
register int N, K;
memset(visit, false, sizeof(visit));
cin >> N >> K;
cout << bfs(N, K);
return 0;
}
// *&)*@*
이미 체크했던 지점을 flag로 기록하고 또다시 해당 지점에 도착하면 패스하는 방식입니다.
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
토마토 (feat. 백준, 7576번) (0) | 2022.08.21 |
---|---|
나이트의 이동 (feat. 백준, 7562번) (0) | 2022.08.20 |
미로 탐색 (feat. 백준, 2178번) (0) | 2022.08.19 |
유기농 배추 (feat. 백준, 1012번) (0) | 2022.08.19 |
단지번호붙이기 (feat. 백준, 2667번) (0) | 2022.08.19 |
Comments