๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 13913๋ฒˆ - ์ˆจ๋ฐ”๊ผญ์งˆ4

by dkswnkk 2021. 11. 6.

https://www.acmicpc.net/problem/13913

 

13913๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 4

์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 ≤ N ≤ 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 ≤ K ≤ 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ

www.acmicpc.net

#include <iostream>
#include <queue>
#define MAX 100001

using namespace std;


int N,K;    //N=์ˆ˜๋นˆ์ด๊ฐ€ ์žˆ๋Š” ์œ„์น˜, K=๋™์ƒ์ด ์žˆ๋Š” ์œ„์น˜
int visited[MAX];
int sec=MAX;
vector<int>path_print;
int path_memory[MAX];

void bfs(int start,int time){
    queue<pair<int,int>>q;
    q.push({start,0});
    visited[start]=1;

    while(!q.empty()){
        int start=q.front().first;
        int time=q.front().second;
        q.pop();


        if(start==K){
            sec=time;
            int path=K;
            while(path!=N){
                path_print.push_back(path);
                path=path_memory[path];
            }
            path_print.push_back(N);
            return;
        }

        if(start-1>=0&&!visited[start-1]){
            path_memory[start-1]=start;
            visited[start-1]=1;
            q.push({start-1,time+1});
        }

        if(start+1<MAX&&!visited[start+1]){
            path_memory[start+1]=start;
            visited[start+1]=1;
            q.push({start+1,time+1});
        }

        if(start*2<MAX&&!visited[start*2]){
            path_memory[start*2]=start;
            visited[start*2]=1;
            q.push({start*2,time+1});
        }



    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin>>N>>K;

    bfs(N,0);
    cout<<sec<<'\n';
    for(int i=path_print.size()-1; i>=0; i--){
        cout<<path_print[i]<<' ';
    }
    if(path_print.empty()){
        cout<<N<<' '<<K;
    }
}

๋Œ“๊ธ€