結果

問題 No.3 ビットすごろく
ユーザー Masaki Kitaguchi
提出日時 2022-01-11 23:52:17
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,026 bytes
コンパイル時間 12,690 ms
コンパイル使用メモリ 398,120 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-14 12:17:29
合計ジャッジ時間 13,936 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#[macro_export]
macro_rules! setup {
    { mut $input:ident: SplitWhitespace $(,)? } => {
        use std::io::Read;
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf).unwrap();
        let mut $input = buf.split_whitespace();
    };
}

#[macro_export]
macro_rules! parse_next {
    ($str_iter:expr) => {
        $str_iter.next().unwrap().parse().unwrap()
    };
}

fn main() {
    setup! { mut input: SplitWhitespace };

    let n: usize = parse_next!(input);

    let ans = match (|| {
        use std::collections::{HashSet, VecDeque};

        let mut queue = VecDeque::new();
        let mut visited = HashSet::new();
        let mut depth = 0;
        {
            queue.push_back(1);
            visited.insert(1);
        }
        while !queue.is_empty() {
            for _ in 0..queue.len() {
                let i = match queue.pop_front() {
                    Some(i) => i,
                    _ => unreachable!(),
                };

                // Goal
                if i == n {
                    return Some(depth);
                }

                let i_ones = i.count_ones() as usize;

                let mut children = vec![];
                {
                    let j = i + i_ones;
                    if 1 <= j && j <= n {
                        children.push(j);
                    }
                }
                {
                    if !(i <= i_ones) {
                        let j = i - i_ones;
                        if 1 <= j && j <= n {
                            children.push(j);
                        }
                    }
                }
                for &j in children.iter() {
                    if !visited.contains(&j) {
                        queue.push_back(j);
                        visited.insert(j);
                    }
                }
            }
            depth += 1;
        }
        return None;
    })() {
        Some(depth) => depth + 1,
        None => -1,
    };

    println!("{:?}", ans);
}
0