結果

問題 No.3 ビットすごろく
ユーザー lioo
提出日時 2021-03-29 14:50:30
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 823 bytes
コンパイル時間 723 ms
コンパイル使用メモリ 73,448 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-29 09:48:52
合計ジャッジ時間 1,735 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

int movecount(int n){
    int count = 0;
    while(n>0){
        count += n%2;
        n /= 2;
    }
    return count;
}


int main(){
    int dist[10001];
    queue<int> q;
    int N;
    cin>>N;
    for(int i=1; i<=N;i++){
        dist[i]=-1;
    }

    q.push(1);
    dist[1] = 1;

    while(!q.empty()){
        int now = q.front();
        int move = movecount(q.front());
        q.pop();

        if((now-move)>=1){
            if(dist[now-move]==-1){
                dist[now-move] = dist[now]+1;
                q.push(now-move);
            }
        }

        if((now+move)<=N){
            if(dist[now+move]==-1){
                dist[now+move] = dist[now]+1;
                q.push(now+move);
            }
        }

    }
    cout<<dist[N]<<endl;
}
0