結果

問題 No.3 ビットすごろく
ユーザー sanyonfu
提出日時 2019-05-13 20:47:18
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,051 bytes
コンパイル時間 1,430 ms
コンパイル使用メモリ 164,100 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 09:20:15
合計ジャッジ時間 2,449 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int bit(int num){
    int s=0;
    while(num != 0){
        if(num%2 == 1){
        s++;
        }
        num /= 2;

    }
    return s;

}
int main(){

    int N;
    cin >> N;
    queue<int> q;
    q.push(1);
    vector<int> his(N+1);
    his.at(1) =1;


    for(int i = 0; i < N; i++){
                if(q.empty()){
            cout << -1 << endl;
            break;
        }
        if(N == q.front()){
            cout << his.at(q.front()) << endl;
            break;
        }

        if(q.front()+bit(q.front()) <= N){
            if(his.at(q.front()+bit(q.front())) == 0){
                q.push(q.front()+bit(q.front()));
                his.at(q.front()+bit(q.front())) = his.at(q.front()) +1;
            }
        }
        if(q.front()-bit(q.front()) > 1 ){
            if(his.at(q.front()-bit(q.front())) == 0){
                q.push(q.front()-bit(q.front()));
                his.at(q.front()-bit(q.front())) = his.at(q.front()) +1;
            }
        }


        q.pop();
    }
}
0