結果

問題 No.3 ビットすごろく
ユーザー siguma323
提出日時 2016-05-05 12:43:38
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 306 ms / 5,000 ms
コード長 1,513 bytes
コンパイル時間 758 ms
コンパイル使用メモリ 77,820 KB
実行使用メモリ 394,368 KB
最終ジャッジ日時 2024-07-01 07:51:20
合計ジャッジ時間 5,421 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <utility>
#include <cmath>
#include <bitset>

using namespace std;

int now_bit(int num)
{
    bitset<1000> bs(num);
    return bs.count();
}

bool InBounds(int pos,int n)
{
    if(pos > 0 && pos <= n)
        return true;
    else
        return false;
}

int main()
{
    int n;
    cin >> n;

    if(n == 1)
    {
        cout << 1 << endl;
        return 0;
    }

    vector<vector<int>> used(n+1,vector<int>(n+1));
    queue<pair<int,int>> q;
    q.push(make_pair(1 + now_bit(1),1));
    used.at(1).at(1+now_bit(1)) = 1;

    while(!q.empty())
    {
        pair<int,int> index = q.front();
        q.pop();

        if(index.first == n)
        {
            cout << index.second + 1 << endl;
            return 0;
        }

        if(InBounds(index.first + now_bit(index.first),n) && !used.at(index.first).at(index.first + now_bit(index.first)))
        {
            q.push(make_pair(index.first + now_bit(index.first),index.second + 1));
            used.at(index.first).at(index.first + now_bit(index.first)) = 1;
        }

        if(InBounds(index.first - now_bit(index.first),n)  && !used.at(index.first).at(index.first - now_bit(index.first)))
        {
            q.push(make_pair(index.first - now_bit(index.first),index.second + 1));
            used.at(index.first).at(index.first - now_bit(index.first)) = 1;
        }
    }

    cout << -1 << endl;
    return 0;

}
0