Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1-InSange #3

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)κΉŒμ§€ λ°©λ¬Έν–ˆλ˜ 점듀을 λ„£μ–΄ μˆœμ„œλŒ€λ‘œ 좜λ ₯ν•˜κΈ° μœ„ν•΄ μŠ€νƒμ„ μ„ μ–Έ

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사싀 stack의 역할은 vector둜 μ™„λ²½ν•˜κ²Œ λŒ€μ²΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. push()λŠ” push_back(), pop()은 pop_back()에 λŒ€μ‘λ˜μ£ .
vectorλ₯Ό μ“°κ²Œ 되면 λ°°μ—΄ 쀑간에 μœ„μΉ˜ν•œ μš”μ†Œλ„ 접근이 κ°€λŠ₯ν•΄μ„œ μ’‹μŠ΅λ‹ˆλ‹€. stack은 그게 λΆˆκ°€λŠ₯ν•˜κ±°λ“ μš” :)


bool Check(int index)
{ // 이동할 수 μžˆλŠ” 점 λ²”μœ„( 0 <= x <= 100,000 )λ₯Ό λ²—μ–΄λ‚˜λ©΄ μ•ˆλœλ‹€!
if (index < 0 || index > 100000) return false;
return true;
}
Comment on lines +15 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν•¨μˆ˜λͺ…을 μ’€ 더 μ§κ΄€μ μœΌλ‘œ μ•Œ 수 있게 적으면 쒋을 것 κ°™λ„€μš”.
개인적으둜 μ €λŠ” μ•„λž˜μ™€ 같이 μž‘μ„±ν•©λ‹ˆλ‹€.

bool OutOfBound(int index)
{
    return index < 0 || index > 100000;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ°Έκ³ ν•˜κ² μŠ΅λ‹ˆλ‹€!
ν™•μ‹€νžˆ 어디에 ν•„μš”ν•œμ§€ μ’€ 더 λͺ…μ‹œμ μ΄λ©΄ μ’‹κ² λ„€μš©


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 };
Comment on lines +26 to +31
Copy link

@9kyo-hwang 9kyo-hwang Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pair<int, int> visited[100001]; λŒ€μ‹  vector<pair<int, int>> visited;둜 μ“°λ©΄ μ’€ 더 νŽΈλ¦¬ν•  것 κ°™μŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄

visited.assign(100001, {MAX_INT, MAX_INT})

이런 μ‹μœΌλ‘œ κ°„νŽΈν•˜κ²Œ μ΄ˆκΈ°ν™”ν•  수 μžˆκ±°λ“ μš” :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ™μ μœΌλ‘œ 데이터λ₯Ό μΆ”κ°€ν• λ•Œλ₯Ό μ œμ™Έν•˜κ³ λŠ” vectorλ₯Ό 잘 μ•ˆμ“°λ‹€λ³΄λ‹ˆ ν•΄λ‹Ή λ°©μ‹μ˜ μ΄ˆκΈ°ν™”λŠ” λͺ°λžλ„€μš”!
쒋은 정보 ν•˜λ‚˜ μ•Œμ•„κ°‘λ‹ˆλ‹€~

}

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
Comment on lines +41 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사싀 return을 κ±Έμ–΄μ€¬μœΌλ©΄ else if둜 λΆ„κΈ°ν•  ν•„μš”λŠ” μ—†μ£ .

if (N == K)
{
    ...
    return;
}

if(N > K)
{
    ...
    return;
}

...
return;

이런 μ‹μœΌλ‘œ ν•˜λ©΄ μ•„λž˜ else λΆ„κΈ°μ˜ indentλ₯Ό 쀄일 수 μžˆμ„ 것 κ°™λ„€μš”.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ‚¬λžŒλ“€μ—κ²Œ 3가지 방법에 λŒ€ν•΄μ„œ μ’€ 더 μ§κ΄€μ μœΌλ‘œ 보여쀄 수 μžˆλŠ” 방법이 뭐가 μžˆμ„κΉŒ κ³ λ €ν•˜λ‹€λ³΄λ‹ˆ κΉœλΉ‘ν–ˆμŠ΅λ‹ˆλ‹€!
지적 κ°μ‚¬ν•©λ‹ˆλ‹€~

{
// 수빈이(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;
}
Comment on lines +65 to +89

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš”κ±° λ°˜λ³΅λ¬Έμ„ μ΄μš©ν•˜λ©΄ μ½”λ“œ 쀑볡을 쀄일 수 μžˆμŠ΅λ‹ˆλ‹€.

for(int offset_num : {-1, +1, cur_num})
{
    int next_num = cur_num + offset_num;
    if(!Check(next_num) || visited[next_num] <= cur_cnt + 1)
    {
        continue;
    }
    q.push(next_num);
    visited[next_num] = {cur_num, cur_cnt + 1};
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ΄κ±°λŠ” ν™•μ‹€νžˆ λ„μ›€μ΄λ˜λ„€μš”!
for문을 많이 μ‚¬μš©ν•˜μ§€λ§Œ κ°€μž₯ 기본적으둜만 ν™œμš©ν•˜λ‹€λ³΄λ‹ˆ μ΄λ ‡κ²Œ λ‹€μ–‘ν•˜κ²Œ μ ‘κ·Όν•˜λŠ” 방법은 μ΅μˆ™μΉ˜ μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€.
ν™•μ‹€νžˆ μ’€ 더 λͺ…ν™•ν•˜κ³  짧게 확인할 수 μžˆλ‹€λŠ” μ μ—μ„œ μ’‹λ„€μš”.
μŠ΅κ΄€μ„ 듀여보도둝 λ…Έλ ₯ν•˜κ² μŠ΅λ‹ˆλ‹€.

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) | - |
=======
---