๐Ÿ™‡โ€โ™€๏ธ[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;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: