結果

問題 No.3 ビットすごろく
ユーザー yanaoyanao
提出日時 2020-10-15 15:06:26
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,755 bytes
コンパイル時間 611 ms
コンパイル使用メモリ 143,488 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 01:09:47
合計ジャッジ時間 1,850 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::collections::VecDeque;

fn getline() -> String{
	let mut __ret=String::new();
	std::io::stdin().read_line(&mut __ret).ok();
	return __ret;
}

fn main() {
    let n: usize = getline().trim().parse().unwrap();

    // すごろくマス
    let mut masu: Vec<i32> = Vec::new();

    // 現在の位置
    let mut current_position: usize = 1;

    // キュー
    let mut queue: VecDeque<usize> = VecDeque::new();
    
    // 1-Nのすごろくマスの初期化
    for _ in 0..n {
        masu.push(-1);
    }
    masu[current_position - 1] = 1;
    queue.push_back(current_position);

    while !queue.is_empty() {
        // bitの1が立ってる数を取得
        let bit_count_ones: usize = queue.pop_front().unwrap().count_ones() as usize;

        let pos = current_position - 1;
        if current_position - bit_count_ones > 0 && masu[pos - bit_count_ones] == -1  {
            // すごろくを前に進もうとする(マス内にいるか、まだとおっていないマスであれば、現在位置を更新する)
            masu[pos - bit_count_ones] = masu[pos] + 1;
            current_position = current_position - bit_count_ones;
            // キューに現在位置をpushする
            queue.push_back(current_position);
        }
        if current_position + bit_count_ones <= n && masu[pos + bit_count_ones] == -1 {
            // すごろくを後に進もうとする(ゴールより前かつまだ通っていないマス)
            masu[pos + bit_count_ones] = masu[pos] + 1;
            current_position = current_position + bit_count_ones;
            // キューに現在位置をpushする
            queue.push_back(current_position);
        }
    }

    println!("{}", masu[n - 1]);
}
0