Skip to content

Commit

Permalink
Merge pull request #3 from AlgoLeadMe/1-InSange
Browse files Browse the repository at this point in the history
1-InSange
  • Loading branch information
InSange authored Mar 14, 2024
2 parents 77cf8a7 + 2afe394 commit 982569f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
118 changes: 118 additions & 0 deletions InSange/BFS/13913.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <iostream>
#include <queue>
#include <stack>
#include <cstring>

using namespace std;

const int MAX_INT = 9999999;

int N, K;
pair<int, int> visited[100001];
queue<int> q; // ํ˜„์žฌ ์ด๋™ํ•ด์•ผํ•  ์นธ๋“ค์„ BFS๋กœ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ๋ฅผ ์„ ์–ธ
stack<int> st; // ๋„์ฐฉ์ง€(K)์—์„œ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ†ตํ•ด ์ถœ๋ฐœ์ง€(N)๊นŒ์ง€ ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ ๋“ค์„ ๋„ฃ์–ด ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ์Šคํƒ์„ ์„ ์–ธ

bool Check(int index)
{ // ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์  ๋ฒ”์œ„( 0 <= x <= 100,000 )๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ์•ˆ๋œ๋‹ค!
if (index < 0 || index > 100000) return false;
return true;
}

void Init()
{ // ์ˆ˜๋นˆ์ด(N : ์ถœ๋ฐœ์ง€)์™€ ๋™์ƒ(K : ๋ชฉ์ ์ง€) ๊ฐ’ ์„ค์ •
cin >> N >> K;
// 0 ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋„ ๋ฒ”์œ„์— ์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์— MAX_INT๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.
// ์ฒซ๋ฒˆ์งธ ๊ฐ’์€ ํ•ด๋‹น ๋ฒˆํ˜ธ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ธฐ ์ด์ „์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•˜๊ณ , ๋‘๋ฒˆ์งธ ๊ฐ’์€ ํ•ด๋‹น ๋ฒˆํ˜ธ๋กœ ์˜ค๊ธฐ๊นŒ์ง€์˜ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.
for (int i = 0; i < 100001; i++)
{
visited[i] = { MAX_INT, MAX_INT };
}
// ์ถœ๋ฐœ ์ง€์ ์€ 0๋ฒˆ ํšŸ์ˆ˜๋กœ ์ฑ„์›Œ๋„ฃ๊ธฐ
visited[N] = { N, 0 };
}

void Solve()
{ // 3๊ฐ€์ง€์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜
if (N == K) // 1. ์ˆ˜๋นˆ(N)๊ณผ ๋™์ƒ(K)๊ฐ€ ๊ฐ™์€ ์œ„์น˜์— ์žˆ์„ ๊ฒฝ์šฐ ์ด๋™ํ•˜๋Š” ์นธ์€ 0์ด๊ณ  ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
{
cout << visited[N].second << "\n" << N;
return;
}
else if (N > K) // 2. ์ˆ˜๋นˆ(N)์ด ๋™์ƒ(K)๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ K๋กœ ๊ฐ์†Œํ•˜๋Š” ์ ‘๊ทผ์€ -1 ๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์— -1๋กœ๋งŒ ๊ณ„์‚ฐํ•œ ๊ฐ’๋“ค์„ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
{
cout << N-K << "\n";
while (N != K)
{
cout << N << " ";
N--;
}
cout << N << "\n";
return;
}
else// if(N < K) // 3. ์ˆ˜๋นˆ(N)์ด ๋™์ƒ(K)๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ +1, *2 ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ -1 * 2๋กœ ๋น ๋ฅธ ์ ‘๊ทผ์„ ๋…ธ๋ ค ๋ณผ์ˆ˜๋„ ์žˆ๋‹ค. ex) 5์—์„œ 8๋กœ ๊ฐˆ ๊ฒฝ์šฐ (1) 5 -> 4 -> 8 (2) 5 -> 10 -> 9 -> 4
{
// ์ˆ˜๋นˆ์ด(N: ์ถœ๋ฐœ์ง€) ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.
q.push(N);

while (!q.empty())
{ // ํ˜„์žฌ ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๋ฅผ ํ์—์„œ ๊บผ๋‚ด์ค€๋‹ค.
int cur_num = q.front();
q.pop();
// ํ˜„์žฌ ์ด๋™ํ•œ ์นธ์˜ ์ˆ˜์™€ ๋‹ค์Œ ์ด๋™ํ•  ์ ์— ๋Œ€ํ•œ ์œ„์น˜ ๊ฐ’์„ ์ €์žฅํ•  ๋ณ€์ˆ˜๋“ค์„ ์„ ์–ธํ•ด์ค€๋‹ค.
int cur_cnt, next_num;
cur_cnt = visited[cur_num].second;
// ํ˜„์žฌ ์œ„์น˜์—์„œ -1๋งŒํผ ์ด๋™ํ–ˆ์„ ๊ฒฝ์šฐ.
next_num = cur_num - 1;
if (Check(next_num) && visited[next_num].second > cur_cnt + 1)
{
q.push(next_num);
visited[next_num].first = cur_num;
visited[next_num].second = cur_cnt + 1;
}
if (next_num == K) break;
// ํ˜„์žฌ ์œ„์น˜์—์„œ +1๋งŒํผ ์ด๋™ํ–ˆ์„ ๊ฒฝ์šฐ.
next_num = cur_num + 1;
if (Check(next_num) && visited[next_num].second > cur_cnt + 1)
{
q.push(next_num);
visited[next_num].first = cur_num;
visited[next_num].second = cur_cnt + 1;
}
if (next_num == K) break;
// ํ˜„์žฌ ์œ„์น˜์—์„œ *2๋งŒํผ ์ด๋™ํ–ˆ์„ ๊ฒฝ์šฐ.
next_num = 2 * cur_num;
if (Check(next_num) && visited[next_num].second > cur_cnt + 1)
{
q.push(next_num);
visited[next_num].first = cur_num;
visited[next_num].second = cur_cnt + 1;
}
if (next_num == K) break;
}
// ์ถœ๋ฐœ์ง€์—์„œ ๋„์ฐฉ์ง€๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๊ฑธ๋ฆฐ ์นธ์˜ ์ˆ˜
cout << visited[K].second << "\n";
// ๋„์ฐฉ์ง€์—์„œ ์ถœ๋ฐœ์ง€๊นŒ์ง€ ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ ๋“ค์„ ์ €์žฅํ•˜์—ฌ ์ถœ๋ ฅ ์ €์žฅํ• ๋•Œ๋Š” K->N์ด์ง€๋งŒ ์ถœ๋ ฅํ• ๋•Œ๋Š” N->K๋กœ ์ถœ๋ ฅ์ด ๋จ!
do {
st.push(K);
K = visited[K].first;
} while (K != N);
st.push(N);

while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
}
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Init();
Solve();

return 0;
}
3 changes: 2 additions & 1 deletion InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2023.10.27 | BFS | - | - |
| 1์ฐจ์‹œ | 2024.03.11 | BFS | [์ˆจ๋ฐ”๊ผญ์งˆ 4](https://www.acmicpc.net/problem/13913) | - |
=======
---

0 comments on commit 982569f

Please sign in to comment.