結果

問題 No.3 ビットすごろく
ユーザー mell1
提出日時 2018-11-27 23:32:21
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 916 bytes
コンパイル時間 537 ms
コンパイル使用メモリ 64,816 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 09:11:40
合計ジャッジ時間 1,496 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;
int bpop(int n){
    int m=n%2;
    for(;n/=2;)m+=n%2;
    return m;
}
int main(){
    int a[10001]={0,1},n,i=1,flag=-1,p,m;
    queue<int> que;
    cin>>n;
    que.push(i);
    while (1){
        if (i==n) {
            flag = 0;
            break;
        }
        p = (i+bpop(i)>n)||(i+bpop(i)<=1)?0:i+bpop(i);
        m = (i-bpop(i)<=1)||(i-bpop(i)>n)?0:i-bpop(i);
        if ((!p&&!m)||(a[p]&&a[m])) {
            que.pop();
            if (que.empty()) break;
            i=que.front();
            continue;
        }
        if (p && !a[p]) {
            a[p] = a[i] + 1;
            que.push(p);
        }
        if (m && !a[m]) {
            a[m] = a[i] + 1;
            que.push(m);
        }
        que.pop();
        if (que.empty()) break;
        i=que.front();
    }
    if (flag) cout<<-1<<"\n";
    else cout<<a[i]<<"\n";
    return 0;
}
0