結果

問題 No.3 ビットすごろく
ユーザー syutarosyutaro
提出日時 2017-06-23 23:07:58
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,325 bytes
コンパイル時間 838 ms
コンパイル使用メモリ 71,572 KB
実行使用メモリ 34,956 KB
最終ジャッジ日時 2024-04-14 06:16:50
合計ジャッジ時間 7,271 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <map>
#include <utility>
#include <algorithm>
#define rep(i, n) for(int (i)=0; (i)<(n); ++(i))
#define INF 1000000
using namespace std;

int count_1(int n) {
    int x = 0;
    while(n > 0) {
        x += n % 2;
        n >>= 1;
    }
    return x;
}

typedef vector<pair<int, int> > G;
typedef vector<bool> Visited;

G create_matrix(int n) {
    G g(n+1);
    for(int i=1; i<=n; ++i) {
        int count = count_1(i);
        int a = i - count < 1 ? -1 : i - count;
        int b = i + count > n ? -1 : i + count;
        g[i] = pair<int, int>(a, b);
    }
    return g;
}

int walk(Visited& v, G g, int n, int x, int s) {
    if(x == n) {
        return s;
    }

    v[x] = true;
    int l = INF, r = INF;
    if(g[x].first != -1 && !v[g[x].first]) {
        l = walk(v, g, n, g[x].first, s+1);
    }

    if(g[x].second != -1 && !v[g[x].second]) {
        r = walk(v, g, n, g[x].second, s+1);
    }
    v[x] = false;

    return min(l, r);
}

Visited get_visited(int n) {
    Visited v(n+1);
    rep(i, n) {
        v[i+1] = false;
    }
    return v;
}

int main()
{
    int n;
    cin >> n;
    Visited v = get_visited(n);
    G g = create_matrix(n);
    int r = walk(v, g, n, 1, 1);
    cout << (r == INF ? -1 : r) << endl;
	return 0;
}
0