結果

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

ソースコード

diff #

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

int ret = -1;
int n;
map<int,int> check;

void calc(int cur,int cnt){
    if(cur == n){
        ret = cnt;
    }
    check[cur] = cnt;
    int num = 0;
    int tmp = cur;
    while(tmp > 0){
        if(tmp % 2 == 1)num++;
        tmp /= 2;
    }
    if(cur + num <= n){
        if(check[cur+num] == 0 || check[cur+num] > cnt+1)
        calc(cur+num,cnt+1);
    }
    if(cur - num >= 1){
        if(check[cur-num] == 0 || check[cur-num] > cnt+1)
        calc(cur-num,cnt+1);
    }
}

int main(){
    cin >> n;
    calc(1,1);
    cout << ret << endl;
}
0