結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  kpinkcat | 
| 提出日時 | 2023-10-05 19:16:01 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 4 ms / 5,000 ms | 
| コード長 | 1,065 bytes | 
| コンパイル時間 | 1,747 ms | 
| コンパイル使用メモリ | 201,792 KB | 
| 最終ジャッジ日時 | 2025-02-17 04:32:35 | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
#include <bits/stdc++.h>
#include<iostream>
#include<map>
#include<vector>
#include <algorithm>
#include<math.h>
#include <iomanip>
#include<set>
#include <numeric>
#include<string>
using ll = long long;
using namespace std;
int main(){
    int n, now = 1, ans = 1, min1 = 1e9;
    bool found = false;
    cin >> n;
    set<int> st;
    deque<pair<int, int>> dq;
    if (n != 1) dq.push_back({now, ans});
    else{
        found = true;
        min1 = 1;
    }
    while (!dq.empty()){
        now = dq.front().first;
        ans = dq.front().second;
        dq.pop_front();
        int tmp, cnt = 0;
        if (st.count(now)) continue;
        st.insert(now);
        tmp = now;
        while (tmp){
            if (tmp &1) cnt++;
            tmp >>= 1;
        }
        if (now + cnt == n){
            found = true;
            min1 = min(min1, ++ans);
        }
        if (now - cnt > 1) dq.push_back({now - cnt, ans+1});
        if (now + cnt < n) dq.push_back({now + cnt, ans+1});
    }
    if (found) cout << min1 << endl;
    else cout << -1 << endl;
}
            
            
            
        