結果

問題 No.3 ビットすごろく
ユーザー kpinkcat
提出日時 2023-10-05 19:13:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 997 bytes
コンパイル時間 1,775 ms
コンパイル使用メモリ 202,756 KB
最終ジャッジ日時 2025-02-17 04:32:28
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    dq.push_back({now, ans});
    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;
}
0