結果

問題 No.643 Two Operations No.2
ユーザー Masaki KitaguchiMasaki Kitaguchi
提出日時 2022-01-27 21:49:11
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,669 bytes
コンパイル時間 17,460 ms
コンパイル使用メモリ 377,876 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-26 03:54:20
合計ジャッジ時間 18,540 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

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).ok();
        let mut $input = buf.split_whitespace();
    };
}

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

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

    let x: i64 = parse_next!(input);
    let y: i64 = parse_next!(input);

    let ans = (|| {
        if !(x == 0 || y == 0 || x.abs() == y.abs()) {
            return -1;
        }
        use std::collections::{HashSet, VecDeque};
        let mut queue = VecDeque::new();
        let mut visited = HashSet::new();
        let mut depth = 0;
        {
            let root = (x, y);
            queue.push_back(root);
            visited.insert(root);
        }
        while !queue.is_empty() {
            for _ in 0..queue.len() {
                let (x, y) = match queue.pop_front() {
                    Some(value) => value,
                    None => unreachable!(),
                };
                if x == y {
                    return depth;
                }
                let children = vec![(y, x), (x + y, x - y)];
                for child in children.into_iter() {
                    if visited.contains(&child) {
                        continue;
                    }
                    queue.push_back(child);
                    visited.insert(child);
                }
            }
            depth += 1;
        }
        unreachable!();
    })();

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