結果

問題 No.3 ビットすごろく
ユーザー sinosino
提出日時 2020-03-22 17:11:11
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,306 bytes
コンパイル時間 4,613 ms
コンパイル使用メモリ 145,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 05:58:56
合計ジャッジ時間 7,731 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 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 #

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;
use std::collections::VecDeque;

#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim_end().to_owned()
}

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}
impl<T: Iterator> IteratorExt for T {}

// 隣接リスト



fn main() {
    let n = read!(usize; 1);
    let n = n[0];

    // 隣接リストの作成
    let mut list: Vec<Vec<usize>> = vec![];
    // インデクス0を埋めるためのダミー
    list.push(vec![]);
    for i in 1..n {
        let mut tmp = vec![];
        let d = i.count_ones() as usize;
        if i > d {
            tmp.push(i-d);
        }
        if i + d <= n {
            tmp.push(i+d);
        }
        list.push(tmp);
    }

    // DFS
    // 開始点は1
    let mut visited = vec![];
    let mut reserved: VecDeque<_> = VecDeque::new();
    // (点, 最短手数)
    reserved.push_back((1, 1));
    let ans = 'outer: loop {
        if let Some((point, min)) = reserved.pop_front() {
            visited.push(point);
            for e in list[point].iter() {
                if *e == n {
                    break 'outer min + 1;
                }
                if !visited.contains(e) {
                    reserved.push_back((*e, min+1));
                }
            }    
        }
        else {
            break 'outer -1;
        }
    };

    println!("{}", ans);

}
0