結果

問題 No.3 ビットすごろく
ユーザー LunaLuna
提出日時 2018-06-09 13:48:28
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,518 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 71,680 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-09-13 01:50:32
合計ジャッジ時間 11,771 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,752 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3,917 ms
4,376 KB
testcase_04 AC 141 ms
4,376 KB
testcase_05 TLE -
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 <utility>
#include <queue>
#include <set>

typedef std::pair<int, int> Grid;
typedef std::queue<Grid> GridQueue;

int getOneBitNumber(int n) {
    int count = 0;
    while (n > 0) {
        if (n % 2 == 1) {
            count++;
        }
        n = n / 2;
    }
    return count;
}

int solve(GridQueue& grid_queue, int n) {
    std::set<int> memo;

    while (!grid_queue.empty()) {
        auto tmp_grid = grid_queue.front(); grid_queue.pop();
        if (tmp_grid.first == n) return tmp_grid.second;
        memo.insert(tmp_grid.first);
        int moving_grid = getOneBitNumber(tmp_grid.first);
        int moved_grid[2] = {tmp_grid.first-moving_grid, tmp_grid.first+moving_grid};
        for (int i = 0; i < 2; i++) {
            if (moved_grid[i] > 0 && moved_grid[i] < n+1) {
                bool gone_flag = false;
                for (auto memo_list : memo) {
                    if (memo_list == moved_grid[i]) {
                        gone_flag = true;
                        break;
                    }
                }
                if (!gone_flag) {
                    Grid new_grid(moved_grid[i], tmp_grid.second+1);
                    grid_queue.push(new_grid);
                }
            }
        }
    }

    return -1;
}

int main(int argc, char const* argv[]) {
    int N;
    Grid grid(1, 1);
    GridQueue grid_queue;
    grid_queue.push(grid);

    std::cin >> N;

    int ans = solve(grid_queue, N);
    std::cout << ans << std::endl;
    return 0;
}
0