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

2-yuyu0830 #5

Merged
merged 2 commits into from
Mar 18, 2024
Merged

2-yuyu0830 #5

merged 2 commits into from
Mar 18, 2024

Conversation

yuyu0830
Copy link
Collaborator

πŸ”— 문제 링크

λ°±μ€€ 12851 μˆ¨λ°”κΌ­μ§ˆ 2

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

3μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

이전 문제λ₯Ό ν’€κ³  κ·ΈλŒ€λ‘œ κ°€μ Έμ™”λ‹€ λ³΄λ‹ˆ 쑰금 정리가 μ•ˆλœ 뢀뢄이 μžˆμŠ΅λ‹ˆλ‹€.

λΈ”λ‘œκ·Έ ν•΄μ„€ 링크

μˆ˜λΉˆμ΄μ™€ 동생은 μˆ˜μ§μ„  μœ„μ— μžˆλ‹€.
μˆ˜λΉˆμ΄λŠ” μ•ž, λ’€λ‘œ 1μΉΈμ”© μ›€μ§μ΄κ±°λ‚˜ ν˜„μž¬ μΉΈ * 2의 칸으둜 μˆœκ°„μ΄λ™ ν•  수 μžˆλ‹€.
각 μ›€μ§μž„μ€ 1초의 μ‹œκ°„μ΄ μ†Œμš”λœλ‹€.

동생은 움직이지 μ•Šμ„ λ•Œ, μˆ˜λΉˆμ΄κ°€ κ°€μž₯ λΉ λ₯΄κ²Œ 동생을 찾을 수 μžˆλŠ” μ‹œκ°„κ³Ό 경우의 수λ₯Ό κ΅¬ν•˜μž


문제λ₯Ό λ‚˜λˆ λ³΄μž.

  1. μˆ˜λΉˆμ΄κ°€ κ°€μž₯ λΉ λ₯΄κ²Œ 동생을 찾을 수 μžˆλŠ” μ‹œκ°„
  2. 1번의 μ‹œκ°„μ— 도착할 수 μžˆλŠ” 경우의 수

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> (); 이런 μ‹μœΌλ‘œ λΉ„μšΈ 수 μžˆλ‹€!

@9kyo-hwang
Copy link

흠 항상 Visited 체크λ₯Ό λ‹€μŒ μΉΈ(Nx) 검사할 λ•Œ ν–ˆμ—ˆμ–΄μ„œ κ·Έλ ‡κ²Œ ν–ˆλŠ”λ° 계속 λΉ κΎΈλ‹Ήν–ˆλ„€μš”.
이런 μ‹μœΌλ‘œ 해버리면 1 + 1μ΄λž‘ 1 * 2λ₯Ό 검사할 λ•Œ μ•žμ—μ„œ 이미 Visited 체크가 λΌλ²„λ¦¬λ‹ˆ μΉ΄μš΄νŒ…μ΄ μ œλŒ€λ‘œ λ˜μ§€ μ•Šλ„€μš”...

#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λ₯Ό μ²΄ν¬ν•΄μ€˜μ•Ό λ˜‘λ°”λ‘œ λ™μž‘ν•˜λŠ” κ±Έ μž‘μ•„λ‚΄λŠ” 데 ν•œμ°Έ κ±Έλ Έκ΅°μš”...

Copy link
Collaborator

@InSange InSange left a 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값이 λ“€μ–΄μ˜€κ²Œ 되면 λ°©λ¬Έν•  ν•„μš”λ„ 없이 λ„˜μ–΄ κ°€μ£ΌλŠ” λ°©μ‹μœΌλ‘œ ν’€μ–΄μ€¬μŠ΅λ‹ˆλ‹€.

yuyu0830/탐색/12851.cpp Outdated Show resolved Hide resolved
@yuyu0830 yuyu0830 requested a review from InSange March 17, 2024 07:10
Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

GOOOD

Copy link
Collaborator

@seongwon030 seongwon030 left a 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)

Copy link
Contributor

@dhlee777 dhlee777 left a 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>> ();
Copy link
Contributor

Choose a reason for hiding this comment

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

큐λ₯Ό λΉ„μš°λŠ” ν•¨μˆ˜κ°€ μ—†κΈ°λ•Œλ¬Έμ— μ΄λ ‡κ²Œ λΉ„μ›Œμ£ΌλŠ”κ΅°μš”.! λ‹€μŒμ— λ¬Έμ œν’€λ•Œ μ°Έκ³ ν•΄λ΄μ•Όκ² μŠ΅λ‹ˆλ‹€ !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants