백준 13549번 (숨바꼭질 3, C++) [BAEKJOON]

숨바꼭질 3

https://www.acmicpc.net/problem/13549

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초512 MB72576207011337524.997%

문제

수빈이는 동생과 숨바꼭질을 하고 있다.

수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 

수빈이는 걷거나 순간이동을 할 수 있다.

만약, 수빈이의 위치가 X일 때 걷는다면 1초 후에 X-1 또는 X+1로 이동하게 된다.

순간이동을 하는 경우에는 0초 후에 2*X의 위치로 이동하게 된다.

수빈이와 동생의 위치가 주어졌을 때,

수빈이가 동생을 찾을 수 있는 가장 빠른 시간이 몇 초 후인지 구하는 프로그램을 작성하시오.

입력

첫 번째 줄에 수빈이가 있는 위치 N과 동생이 있는 위치 K가 주어진다. 

N과 K는 정수이다.

출력

수빈이가 동생을 찾는 가장 빠른 시간을 출력한다.

예제 입력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5 17
5 17
5 17

예제 출력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
2
2
2

힌트

수빈이가 5-10-9-18-17 순으로 가면 2초만에 동생을 찾을 수 있다.

추가 반례

https://www.acmicpc.net/board/search/all/problem/13549

예제 입력 A

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 2
1 2
1 2

예제 출력 A

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0

예제 입력 B

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 17
1 17
1 17

예제 출력 B

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
1
1

예제 입력 C

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
4 6
4 6
4 6

예제 출력 C

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
1
1

예제 입력 D

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0 100000
0 100000
0 100000

예제 출력 D

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
6
6
6

출처

비슷한 문제

알고리즘 분류


통과된 코드

Deque을 이용한 탐색과 순서에 주의 (순간 이동은 시간이 증가하지 않는다.)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <deque>
using namespace std;
deque<pair<int, int>> _MyDeque;
int _N, _K, _Res;
bool _isVisted[100001];
int main()
{
cin >> _N >> _K;
_isVisted[_N] = true;
_MyDeque.push_back(make_pair(_N, 0));
while (!_MyDeque.empty()) {
pair<int, int> _Temp = _MyDeque.front();
_MyDeque.pop_front();
int _TempPos, _CntPos = _Temp.first;
int _CntTime = _Temp.second;
_isVisted[_Temp.first] = true;
if (_Temp.first == _K) {
_Res = _Temp.second;
break;
}
_TempPos = _CntPos * 2;
if (_TempPos <= 100000 && !_isVisted[_TempPos]) {
_MyDeque.push_front(make_pair(_TempPos, _CntTime));
}
_TempPos = _CntPos + 1;
if (_TempPos <= 100000 && !_isVisted[_TempPos]) {
_MyDeque.push_back(make_pair(_TempPos, _CntTime + 1));
}
_TempPos = _CntPos - 1;
if (_TempPos >= 0 && !_isVisted[_TempPos]) {
_MyDeque.push_back(make_pair(_TempPos, _CntTime + 1));
}
}
cout << _Res;
return 0;
}
#include <iostream> #include <deque> using namespace std; deque<pair<int, int>> _MyDeque; int _N, _K, _Res; bool _isVisted[100001]; int main() { cin >> _N >> _K; _isVisted[_N] = true; _MyDeque.push_back(make_pair(_N, 0)); while (!_MyDeque.empty()) { pair<int, int> _Temp = _MyDeque.front(); _MyDeque.pop_front(); int _TempPos, _CntPos = _Temp.first; int _CntTime = _Temp.second; _isVisted[_Temp.first] = true; if (_Temp.first == _K) { _Res = _Temp.second; break; } _TempPos = _CntPos * 2; if (_TempPos <= 100000 && !_isVisted[_TempPos]) { _MyDeque.push_front(make_pair(_TempPos, _CntTime)); } _TempPos = _CntPos + 1; if (_TempPos <= 100000 && !_isVisted[_TempPos]) { _MyDeque.push_back(make_pair(_TempPos, _CntTime + 1)); } _TempPos = _CntPos - 1; if (_TempPos >= 0 && !_isVisted[_TempPos]) { _MyDeque.push_back(make_pair(_TempPos, _CntTime + 1)); } } cout << _Res; return 0; }
#include <iostream>
#include <deque>

using namespace std;

deque<pair<int, int>> _MyDeque;
int _N, _K, _Res;
bool _isVisted[100001];

int main()
{
	cin >> _N >> _K;
	_isVisted[_N] = true;
	_MyDeque.push_back(make_pair(_N, 0));
	while (!_MyDeque.empty()) {
		pair<int, int> _Temp = _MyDeque.front();
		_MyDeque.pop_front();
		int _TempPos, _CntPos = _Temp.first;
		int _CntTime = _Temp.second;
		_isVisted[_Temp.first] = true;
		if (_Temp.first == _K) {
			_Res = _Temp.second;
			break;
		}

		_TempPos = _CntPos * 2;
		if (_TempPos <= 100000 && !_isVisted[_TempPos]) {
			_MyDeque.push_front(make_pair(_TempPos, _CntTime));
		}

		_TempPos = _CntPos + 1;
		if (_TempPos <= 100000 && !_isVisted[_TempPos]) {
			_MyDeque.push_back(make_pair(_TempPos, _CntTime + 1));
		}

		_TempPos = _CntPos - 1;
		if (_TempPos >= 0 && !_isVisted[_TempPos]) {
			_MyDeque.push_back(make_pair(_TempPos, _CntTime + 1));
		}
	}

	cout << _Res;
	return 0;
}

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다