結果

問題 No.3 ビットすごろく
ユーザー KuphonyKuphony
提出日時 2016-03-17 00:08:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,152 ms / 5,000 ms
コード長 2,207 bytes
コンパイル時間 707 ms
コンパイル使用メモリ 68,116 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-13 23:47:47
合計ジャッジ時間 11,209 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 28 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 10 ms
4,376 KB
testcase_09 AC 164 ms
4,376 KB
testcase_10 AC 441 ms
4,376 KB
testcase_11 AC 101 ms
4,376 KB
testcase_12 AC 23 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 369 ms
4,376 KB
testcase_15 AC 1,000 ms
4,376 KB
testcase_16 AC 569 ms
4,376 KB
testcase_17 AC 893 ms
4,384 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 1,152 ms
4,460 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 389 ms
4,376 KB
testcase_23 AC 1,147 ms
4,488 KB
testcase_24 AC 1,147 ms
4,444 KB
testcase_25 AC 998 ms
4,380 KB
testcase_26 AC 1 ms
4,500 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 536 ms
4,376 KB
testcase_29 AC 107 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 64 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
using namespace std;
//1の数をカウント
int count1(int x) {
    int count = 0;
    while(x) {
        if (x&1)
            count++;
        x>>=1;
    }
    return count;
}

int main(int argc, const char * argv[]) {
    int n;
    cin >> n;
    queue<int> qu;
    queue<int> tmpqu;
    int init = 1;
    int tmp;
    qu.push(init);
    int count = 1;
    bool flag = false;
    bool flag2 = false;
    bool passed[10001];
    for (int i = 0; i < 10001; i++) {
        passed[i] = false;
    }
    if(n == 1){
        flag = true;
    }
    //探索
    while(!flag){
        //深さを更新
        count++;
        //プラスマイナス両方の方向を調べる
        while(!qu.empty()){
            tmp = qu.front();
            qu.pop();
            //プラス
            //到達した場合はフラグを立てる
            if(tmp + count1(tmp) == n){
                flag2 = true;
                flag = true;
            }
            else if (tmp + count1(tmp) > n){
            }
            else{
                if(!passed[tmp + count1(tmp)]){
                    tmpqu.push(tmp + count1(tmp));
                    //cout << (tmp + count1(tmp));
                    //cout << "\n";
                }
            }
            //マイナス
            if(tmp - count1(tmp) == n){
                flag2 = true;
                flag = true;
            }
            else{
                if(tmp - count1(tmp) > 0){
                    if(!passed[tmp - count1(tmp)]){
                        tmpqu.push(tmp - count1(tmp));
                        //cout << (tmp - count1(tmp));
                        //cout << "\n";
                    }
                }
            }
        }
        //既存のしかない場合
        if(tmpqu.empty()){
            if(!flag2){
                count = -1;
            }
            flag = true;
        }
        //まとめてプッシュ
        while(!tmpqu.empty()){
            tmp = tmpqu.front();
            tmpqu.pop();
            passed[tmp] = true;
            qu.push(tmp);
        }
    }
    cout << count;
    cout << "\n";
}
0