백준 13913 숨바꼭질 4 ❌

CJB_ny·2023년 2월 19일
0

백준

목록 보기
75/104
post-thumbnail

숨바꼭질 4


일단 놇친 부분은

수빈이 위치가 60000쯤될 경우 동생위치가 100000만일 때

12만으로 갔다가 뒤로 빠꾸 치는게 더 빠를 수 있다.

그래서 MAX값이 200004정도 되어야한다.

또한 기억해야 할 부분이

prev[next] = here

trace 추적할 때 사용하도록 하자.


#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define endl "\n"
#define MAX 100004
#define prev aaa
#define next aaaa

const int max_n = 200004;
int visited[max_n], prev[max_n], n, k, ret, here, cnt, next;
vector<int> v;
queue<int> q;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n >> k;
    visited[n] = 1;
    q.push(n);

    while (!q.empty())
    {
        here = q.front(); q.pop();
        if (here == k)
        {
            ret = visited[k];
            break;
        }
        for (int next : {here + 1, here - 1, here * 2})
        {
            if (next >= max_n || next < 0 || visited[next]) continue;
            visited[next] = visited[here] + 1;
            prev[next] = here;
            q.push(next);
        }
    }
    for (int i = k; i != n; i = prev[i])
    {
        v.push_back(i);
    }

    v.push_back(n);
    cout << ret - 1 << endl;
    reverse(v.begin(), v.end());
    for (int i : v) cout << i << " ";

    return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글