πŸ™‡β€β™€οΈ[Silver I] μˆ¨λ°”κΌ­μ§ˆ - 1697

문제 링크

μ„±λŠ₯ μš”μ•½

λ©”λͺ¨λ¦¬: 2676 KB, μ‹œκ°„: 0 ms

λΆ„λ₯˜

λ„ˆλΉ„ μš°μ„  탐색, κ·Έλž˜ν”„ 이둠, κ·Έλž˜ν”„ 탐색

제좜 일자

2024λ…„ 1μ›” 3일 19:38:38

문제 μ„€λͺ…

μˆ˜λΉˆμ΄λŠ” 동생과 μˆ¨λ°”κΌ­μ§ˆμ„ ν•˜κ³  μžˆλ‹€. μˆ˜λΉˆμ΄λŠ” ν˜„μž¬ 점 N(0 ≀ N ≀ 100,000)에 있고, 동생은 점 K(0 ≀ K ≀ 100,000)에 μžˆλ‹€. μˆ˜λΉˆμ΄λŠ” κ±·κ±°λ‚˜ μˆœκ°„μ΄λ™μ„ ν•  수 μžˆλ‹€. λ§Œμ•½, 수빈이의 μœ„μΉ˜κ°€ X일 λ•Œ κ±·λŠ”λ‹€λ©΄ 1초 후에 X-1 λ˜λŠ” X+1둜 μ΄λ™ν•˜κ²Œ λœλ‹€. μˆœκ°„μ΄λ™μ„ ν•˜λŠ” κ²½μš°μ—λŠ” 1초 후에 2*X의 μœ„μΉ˜λ‘œ μ΄λ™ν•˜κ²Œ λœλ‹€.

μˆ˜λΉˆμ΄μ™€ λ™μƒμ˜ μœ„μΉ˜κ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, μˆ˜λΉˆμ΄κ°€ 동생을 찾을 수 μžˆλŠ” κ°€μž₯ λΉ λ₯Έ μ‹œκ°„μ΄ λͺ‡ 초 후인지 κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

μž…λ ₯

첫 번째 쀄에 μˆ˜λΉˆμ΄κ°€ μžˆλŠ” μœ„μΉ˜ Nκ³Ό 동생이 μžˆλŠ” μœ„μΉ˜ Kκ°€ 주어진닀. Nκ³Ό KλŠ” μ •μˆ˜μ΄λ‹€.

좜λ ₯

μˆ˜λΉˆμ΄κ°€ 동생을 μ°ΎλŠ” κ°€μž₯ λΉ λ₯Έ μ‹œκ°„μ„ 좜λ ₯ν•œλ‹€.

πŸš€ν’€μ΄

μ²˜μŒμ— DFS둜 ν•˜λ €κ³  ν–ˆλŠ”λ° DFS둜 ν•˜λ©΄ μ•ˆλœλ‹€λŠ”κ±Έ μ•Œμ•˜λ‹€.

+1, -1, *2 ν•˜λŠ” 값듀이 λ ˆλ²¨λ³„λ‘œ λ‚΄λ €μ˜€λ‹ˆκΉŒ 트리 ꡬ쑰둜 μƒκ°ν•΄μ„œ ν’€μ—ˆλ‹€.

그리고 λ§Œμ•½ 5μ—μ„œ μ‹œμž‘ν•΄μ„œ -1ν•œ κ°’μœΌλ‘œ 4κ°€ 되고 λ‹€μ‹œ +1을 ν•΄μ„œ 5κ°€λ˜λ©΄ 의미 μ—†λŠ” λ°˜λ³΅μ΄λ‹ˆ λ‹€μ‹œ λ˜λŒμ•„κ°€μ§€ μ•Šκ²Œ λ§Œλ“€μ–΄μ€˜μ•Όν•œλ‹€.

ν•œλ§ˆλ””λ‘œ BFS둜 ν•΄κ²°ν•΄μ•Όν•˜λŠ” λ¬Έμ œμ΄λ‹€.

int n, k;
int discovered[100001];
void BFS(int here)
{
	queue<pair<int, int>> q;
	q.push({ here, 0 });
	discovered[n] = true;
	while (q.empty() == false)
	{
		int here = q.front().first;
		int cnt = q.front().second;
		q.pop();
		if (here == k)
		{
			cout << cnt;
			return;
		}
		if (here + 1 < 100001)
		{
			if (discovered[here + 1] == false)
			{
				discovered[here + 1] = true;
				q.push({ here + 1, cnt + 1 });
			}
		}
		if (here - 1 >= 0)
		{
			if (discovered[here - 1] == false)
			{
				discovered[here - 1] = true;
				q.push({ here - 1, cnt + 1 });
			}
		}
		if (here * 2 >= 0 && here * 2 < 100001)
		{
			if (discovered[here * 2] == false)
			{
				discovered[here * 2] = true;
				q.push({ here * 2, cnt + 1 });
			}
		}
	}
}

πŸš€μ „μ²΄ μ½”λ“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int n, k;
int discovered[100001];
void BFS(int here)
{
	queue<pair<int, int>> q;
	q.push({ here, 0 });
	discovered[n] = true;
	while (q.empty() == false)
	{
		int here = q.front().first;
		int cnt = q.front().second;
		q.pop();
		if (here == k)
		{
			cout << cnt;
			return;
		}
		if (here + 1 < 100001)
		{
			if (discovered[here + 1] == false)
			{
				discovered[here + 1] = true;
				q.push({ here + 1, cnt + 1 });
			}
		}
		if (here - 1 >= 0)
		{
			if (discovered[here - 1] == false)
			{
				discovered[here - 1] = true;
				q.push({ here - 1, cnt + 1 });
			}
		}
		if (here * 2 >= 0 && here * 2 < 100001)
		{
			if (discovered[here * 2] == false)
			{
				discovered[here * 2] = true;
				q.push({ here * 2, cnt + 1 });
			}
		}
	}
}

void solve()
{
	cin >> n >> k;

	BFS(n);
}

int main() 
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

νƒœκ·Έ:

μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ: