-
Notifications
You must be signed in to change notification settings - Fork 0
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
1-InSange #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ν¨μλͺ
μ μ’ λ μ§κ΄μ μΌλ‘ μ μ μκ² μ μΌλ©΄ μ’μ κ² κ°λ€μ. bool OutOfBound(int index)
{
return index < 0 || index > 100000;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
visited.assign(100001, {MAX_INT, MAX_INT}) μ΄λ° μμΌλ‘ κ°νΈνκ² μ΄κΈ°νν μ μκ±°λ μ :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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λ₯Ό μ€μΌ μ μμ κ² κ°λ€μ. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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};
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄κ±°λ νμ€ν λμμ΄λλ€μ! |
||
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; | ||
} |
There was a problem hiding this comment.
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μ κ·Έκ² λΆκ°λ₯νκ±°λ μ :)