結果

問題 No.3 ビットすごろく
ユーザー 4885rhkA4885rhkA
提出日時 2015-07-06 08:11:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 3,369 bytes
コンパイル時間 658 ms
コンパイル使用メモリ 88,244 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 23:14:23
合計ジャッジ時間 1,998 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 5 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
//  main.cpp
//  Q11
//
//  Created by AkihiroKOBAYASHI on 7/5/15.
//  Copyright (c) 2015 Akhr5884. All rights reserved.
//

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <iomanip>

using namespace std;

string to_binString(unsigned int val){
    if( !val ) {
        return string("0");
    }
    string str;
    while( val != 0 ) {
        if( (val & 1) == 0 )  // val は偶数か?
            str.insert(str.begin(), '0');  //  偶数の場合
        else
            str.insert(str.begin(), '1');  //  奇数の場合
        val >>= 1;
    }
    return str;
}

int canGoValue(string binString) {
    int i;
    int value = 0;
    for(i = 0; i < binString.size(); i++) {
        if(binString[i] == '1') {
            value++;
        }
    }
    return value;
}

int main(int argc, const char * argv[]) {
    using namespace std;
    int goalvalue;
    int *goalvalueArray;
    int i = 0;
    queue<int> queue;
    
    cin >> goalvalue;
    goalvalueArray = (int*)malloc(sizeof(goalvalueArray) * (goalvalue + 1));
    
    while(i <= goalvalue) {
        goalvalueArray[i] = -1;
        i++;
    }
    
    queue.push(1);
    i = 1;
    goalvalueArray[1] = 1;

    while (queue.size()) {
        
        int i = queue.front();
        queue.pop();

        int n = i + canGoValue(to_binString(i));
        if(n > 0 && n <= goalvalue && goalvalueArray[n] == -1){
            queue.push(n);
            goalvalueArray[n] = goalvalueArray[i] + 1;
        }
        
        n = i - canGoValue(to_binString(i));
        if(n > 0 && n <= goalvalue && goalvalueArray[n] == -1) {
            queue.push(n);
            goalvalueArray[n] = goalvalueArray[i] + 1;
        }

        
    }
    
    cout << goalvalueArray[goalvalue] << "\n";
    
    
    return 0;
}

//#define _CRT_SECURE_NO_WARNINGS
//#define _USE_MATH_DEFINES
//#include <iostream>
//#include <map>
//#include <set>
//#include <list>
//#include <cstdio>
//#include <cmath>
//#include <cstring>
//#include <string>
//#include <vector>
//#include <algorithm>
//#include <utility>
//#include <queue>
//#include <iomanip>
//
//#define SIZE 10001
//
//using namespace std;
//
///* 1のビット数 */
//int numofbits(long bits)
//{
//    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
//    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
//    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
//    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
//    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
//}
//
//int main()
//{
//    int d[SIZE];
//    int N;
//    queue<int> que;
//    
//    cin >> N;
//    
//    memset(d, -1, sizeof(d));
//    
//    que.push(1);
//    d[1] = 1;
//    
//    while (que.size()) {
//        int p = que.front();
//        que.pop();
//        
//        int n = p + numofbits(p);
//        if (0 < n && n <= N && d[n] == -1) {
//            que.push(n);
//            d[n] = d[p] + 1;
//        }
//        n = p - numofbits(p);
//        if (0 < n && n <= N && d[n] == -1) {
//            que.push(n);
//            d[n] = d[p] + 1;
//        }
//    }
//    
//    cout << d[N] << endl;
//}
0