-
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
2-yuyu0830 #5
2-yuyu0830 #5
Conversation
ν νμ Visited 체ν¬λ₯Ό λ€μ μΉΈ(Nx) κ²μ¬ν λ νμμ΄μ κ·Έλ κ² νλλ° κ³μ λΉ κΎΈλΉνλ€μ. #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int N, K; cin >> N >> K;
auto OutOfBound = [&](int X)
{
return X < 0 || X > 100000;
};
deque<pair<int, int>> Q;
vector<bool> Visited(100001, false);
Q.emplace_back(N, 0);
Visited[N] = true;
int AnswerTick = 100001, AnswerSize = 0;
while(!Q.empty())
{
const auto [X, Tick] = Q.front();
Q.pop_front();
if(X == K)
{
if(Tick < AnswerTick)
{
AnswerTick = Tick;
AnswerSize = 1;
}
else if(Tick == AnswerTick)
{
AnswerSize += 1;
}
}
Visited[X] = true; // λ€ μ΄λ!
for(const int Nx : {X - 1, X + 1, X * 2})
{
if(OutOfBound(Nx) || Visited[Nx])
{
continue;
}
Q.emplace_back(Nx, Tick + 1);
}
}
cout << AnswerTick << "\n" << AnswerSize;
return 0;
} νμ¬ μΉΈ Xλ₯Ό λ§λ λλ§λ€ Visitedλ₯Ό 체ν¬ν΄μ€μΌ λλ°λ‘ λμνλ κ±Έ μ‘μλ΄λ λ° νμ°Έ κ±Έλ Έκ΅°μ... |
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.
μ¬λ¬ λΆκ° 쑰건μ μΆκ°νμμ§λ§ μΌμ μ λ§μμ£Όμ
¨λ μ°μ μμνλ‘ ν΄κ²°νλ λ°©λ²μ λν΄μ μ‘°κΈμ΄λλ§ μ΄ν΄κ° κ°λκ΅°μ.
νΉμ μκ°μ μ¬μ κ° μμΌμλ€λ©΄ μ£Όμλ λΆνλλ €λ λ κΉμ?
ν΄μνλλ° μ½κ°μ μ΄λ €μμ λμμ΄ λ μ μμΌλ©΄ μ’μ κ² κ°μ΅λλ€!
μ 체μ μΈ ν¨μ¨μ±μ λν΄μλ κΆκΈνλ€μ!
μ λ λ€μκ³Ό κ°μ΄ νμμ΅λλ€.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N, K;
vector<int> times; // ν¬μΈνΈλ§λ€ κ±Έλ¦° μκ°
vector<int> visited; // μ λ§λ€ λ°©λ¬Έν μ
queue<int> q;
bool OutOfBounds(int num)
{
return (num < 0 || 100000 < num);
}
void Solve()
{
times.assign(100001, 100001);
visited.assign(100001, 0);
cin >> N >> K;
times[N] = 0;
if (N > K || N == K)
{
cout << N - K << "\n" << 1;
return;
}
q.push(N);
while (!q.empty())
{
int curPoint = q.front();
q.pop();
for (int nextPoint : {curPoint + 1, curPoint - 1, 2 * curPoint})
{
if (OutOfBounds(nextPoint) || times[curPoint]+1 > times[K]) continue;
if (visited[nextPoint] == 0)
{
visited[nextPoint] = 1;
times[nextPoint] = times[curPoint] + 1;
q.push(nextPoint);
}
else if (times[nextPoint] >= times[curPoint] + 1)
{
visited[nextPoint]++;
q.push(nextPoint);
}
}
}
cout << times[K] << "\n" << visited[K];
}
int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);
Solve();
return 0;
}
Kμ λλ¬μ νμλ€λ©΄ νμ¬ κ°μ μκ°μ λΉκ΅ν΄μ κ³μ°μ΄ νμμλ λΆλΆμ μλ΅ν΄μ£Όμκ³ , μ‘°κ±΄μ΄ μλλ©΄ λͺ¨λ λ°©λ¬Έμ κ°λ₯μ±μ μ΄μ΄μ£Όμ΄ λ°©λ¬Έ ν΄μ€¬λ κ² κ°μ΅λλ€.
νμ λ€μ΄κ°λ κ°μ νμ¬ μκ° tμμ λ€μ μκ° t+1μ λ€μ΄κ°λ κ°λ€μ cur+1, cur-1, 2*cur μ΄ 3κ°λ‘ μ°μμ μΈ λΆλΆμ μ§λκΈ° λλ¬Έμ μ΄λ³΄λ€ ν° tκ°μ΄ λ€μ΄μ€κ² λλ©΄ λ°©λ¬Έν νμλ μμ΄ λμ΄ κ°μ£Όλ λ°©μμΌλ‘ νμ΄μ€¬μ΅λλ€.
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.
GOOOD
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.
μ¨λ°κΌμ§ 1μμ λ°μ λ κ²½λ‘ μ μ°ΎκΈ°λ€μ! κΈ°μ‘΄μ μ½λμ λ무 μμ‘΄ν΄μ κ²½λ‘μ μ°ΎκΈ°κ° μ€λ κ±Έλ Έμ΅λλ€. μ λ μλΉμ΄μ λμμ μμΉμ μΌμΉν λ cntλΌλ λ³μμ 1μ λνλ μμΌλ‘ νμ΅λλ€. continueλ₯Ό λ£μ΄ λͺ¨λ κ²½λ‘μλ₯Ό λ€ κ΅¬νλλ‘ νμ΅λλ€.
from collections import deque
result, cnt = 0,0
MAX = 10**5 # μμ§μΌ μ μλ μ΅λμ’ν
dist = [0] * (MAX + 1) # ν΄λΉ μμΉμ λμ°©νμ λ μκ°
n, k = map(int,input().split())
q = deque()
q.append(n)
while q:
x = q.popleft()
if x==k: # xλ³μμΈ μλΉμ μμΉκ° λμμ΄ μλ kμ κ°μ λ λ©μΆ€
result = dist[x]
cnt += 1
continue
# nx = 4,6,10 (νμ¬ μμΉ 5μΌ λ μ΄λν μ μλ λ°©ν₯)
for nx in (x-1, x+1, x*2):
# λ²μλ΄μ μκ³ μμ§ λ°©λ¬Έμνκ±°λ μ΄μ λ°©λ¬Έ + 1μ΄ νμ¬
if 0 <= nx <= MAX and (not dist[nx] or dist[nx] == dist[x]+1):
dist[nx] = dist[x] + 1 # μ΄λν μμΉμ νμ¬ μ΄λν μκ° νμ
q.append(nx)
print(result)
print(cnt)
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.
νλ₯Ό λΉμμ£Όλ λ°©μμ λν΄ μλ‘κ² μκ³ κ°λλ€. μ΄λ² PRλ κ³ μνμ ¨μ΅λλ€ !
|
||
// μ«μκ° μ»€μ Έμ νμμ μ’ λ£νλ©΄ λ¨μ νλ λΉμμ£ΌκΈ° | ||
if (cur > k) { | ||
q[e] = priority_queue <int, vector<int>, greater<int>> (); |
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.
νλ₯Ό λΉμ°λ ν¨μκ° μκΈ°λλ¬Έμ μ΄λ κ² λΉμμ£Όλκ΅°μ.! λ€μμ λ¬Έμ νλ μ°Έκ³ ν΄λ΄μΌκ² μ΅λλ€ !!
π λ¬Έμ λ§ν¬
λ°±μ€ 12851 μ¨λ°κΌμ§ 2
βοΈ μμλ μκ°
3μκ°
β¨ μλ μ½λ
μ΄μ λ¬Έμ λ₯Ό νκ³ κ·Έλλ‘ κ°μ Έμλ€ λ³΄λ μ‘°κΈ μ λ¦¬κ° μλ λΆλΆμ΄ μμ΅λλ€.
λΈλ‘κ·Έ ν΄μ€ λ§ν¬
λ¬Έμ λ₯Ό λλ 보μ.
1λ²μ BFSλ‘ ν΄κ²°νλ©΄ λλ€.
2κ°μ νλ₯Ό μ¬μ©ν΄ ν νλ₯Ό μ¬μ©νλ λμ λ°λ νμ λ€μ νμ ν μμΉλ₯Ό λ£κ³ λ€μ νμμλ λ°λλ‘ μ¬μ©νλ€.
νμ λ€μ΄μλ "μμ§μ΄κ° μμ μ μλ μμΉ" μμ + 1, - 1, * 2 ν μμΉλ₯Ό νμνλ€.
μ΄ λ, μ΅μ μκ°μ νμν΄μΌνκΈ° λλ¬Έμ μΆνμ κ°μ μμΉλ₯Ό λ°©λ¬Ένλ κ²μ μλ―Έκ° μλ€.
visited
λ°°μ΄μ ν΅ν΄ μ€λ³΅ νμμ λ°©μ§νμ2λ²μ΄ μ‘°κΈ ν·κ°λ Έλλ°
nλ²μ§Έ νμμμ κ°μ μμΉμ κ° μ μλ κ²½μ°μ μκ° κ²ΉμΉλ κ²½μ°κ° μλ€.
μλ₯Ό λ€μ΄ 1μμ 2λ₯Ό κ°λ κ²½μ°, 1 + 1, 1 * 2 μ λκ°μ§ κ²½μ°κ° μκΈ΄λ€.
κ·Έλμ dist λΌλ λ°°μ΄μ ν΅ν΄ ν΄λΉ μμΉμ λλ¬ν μ μλ κ²½μ°μ μλ₯Ό λͺ¨μλ€.
μ¬κΈ°μ μ£Όμν μ μ nλ²μ§Έ νμμμ νμνλ μμΉκ° μλλ©΄ κ°μ λ°κΎΈλ©΄ μλλ€.
μκΎΈ κ°μ΄ μ΄μνκ² λμ νμΈν΄λ΄€λλ μ΄μ μ νμνλ νμλ₯Ό μκΎΈ 건λλ¦¬κ³ μμλ€..
μ΄ μ λλ§ ν΄κ²°νλ©΄ λλλ°
100,000μ 10,000 μΌλ‘ μλͺ» μ μ΄μ μκΎΈ μκ°μ΄κ³Όκ° λ΄μλλ°
λ κ·Έκ²λ λͺ¨λ₯΄κ³ λ΄ νμ΄κ° λ¬Έμ μΈ μ€ μκ³ μ΅λν μκ°μ μ€μ΄κΈ° μν΄ λ Έλ ₯νλ€.
κ²°κ΅
priority_queue
λ₯Ό ν΅ν΄ μμ§μ μμΉ μ λ ¬, κ°μ΄cur
λ³΄λ€ μ»€μ§λ©΄ νμ μ’ λ£νλ λ‘μ§μΌλ‘ ν΄κ²°νλ€.κ·Έλ₯
vector
λ‘ νμ΄λ λμλ―?π μλ‘κ² μκ²λ λ΄μ©
μ°μ μμ νλ λ°λ‘ λΉμ°λ ν¨μκ° μλ€.
q = priority_queue<int> ();
μ΄λ° μμΌλ‘ λΉμΈ μ μλ€!