[BOJ] 1697번 숨바꼭질

yeham·2022년 11월 6일
0

백준

목록 보기
5/22

문제

숨바꼭질

코드

#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>

using namespace std;

int board[100001];
int x[3];
int n, m;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	queue<int> q;
	cin >> n >> m;
	board[n] = 1;
	if (n == m)
	{
		cout << 0;
		return (0);
	}
	q.push(n);
	while (!q.empty())
	{
		int cur = q.front();
		q.pop();
		x[0] = cur + 1;
		x[1] = cur - 1;
		x[2] = cur * 2;
		for (int i = 0; i < 3; i++)
		{
			if (x[i] < 0 || x[i] >= 100001)
				continue;
			if (board[x[i]] != 0)
				continue;
			if (x[i] == m)
			{
				cout << board[cur];
				return (0);
			}
			board[x[i]] = board[cur] + 1;
			q.push(x[i]);
		}
	}
	return (0);
}
profile
정통과 / 정처기 & 정통기 / 42seoul 7기 Cardet / 임베디드 SW 개발자

0개의 댓글