하다보니

1697번-숨바꼭질 본문

알고리즘 풀이/백준

1697번-숨바꼭질

claire 2022. 2. 3. 17:13
/*
수빈이는 현재 n, 동생은 k에 있따. 
x+1,x-1가능, 2*x가능. 수빈이와 동생의 위치가 주어졌을 때
수빈이가 동생을 찾을 수 있는 가장 빠른 시간이 몇 초 후인가. 

*/
#include<bits/stdc++.h>
using namespace std;

int dist[100002];

int main() {
	int n, k;
	cin >> n >> k;
	fill(dist, dist + 100001, -1);
	dist[n] = 0;
	queue<int> Q;
	Q.push(n);
	while (dist[k] == -1) {
		int cur = Q.front(); Q.pop();
		for (int i : {cur - 1, cur + 1, cur * 2}) {
			if (i < 0 || i>100000)continue;
			if (dist[i] != -1)continue;
			dist[i] = dist[cur] + 1;
			Q.push(i);

		}
	}
	cout << dist[k];
}

'알고리즘 풀이 > 백준' 카테고리의 다른 글

10026번-적록색약  (0) 2022.02.04
1012번-유기농 배추  (0) 2022.02.03
1697번-불!  (0) 2022.01.30
7576번-토마토  (0) 2022.01.29
2178번-미로탐색  (0) 2022.01.29