結果

問題 No.602 隠されていたゲーム2
ユーザー take4s5itake4s5i
提出日時 2021-12-01 11:35:27
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 2,232 bytes
コンパイル時間 16,113 ms
コンパイル使用メモリ 378,032 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-07-04 10:54:10
合計ジャッジ時間 22,008 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 WA * 6 TLE * 1 -- * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `move_len` is never read
 --> src/main.rs:7:5
  |
6 | struct Input {
  |        ----- field in this struct
7 |     move_len: usize,
  |     ^^^^^^^^
  |
  = note: `Input` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

use std::collections::BTreeSet;
use std::io;
use std::ops::Bound;

#[derive(Debug, Clone)]
struct Input {
    move_len: usize,
    moves: BTreeSet<usize>,
    dest: (i64, i64),
    mt_len: usize,
}

impl Input {
    fn search_dest(&self) -> isize {
        if self.dest == (0, 0) {
            0
        } else if self.moves.contains(&self.mt_len) {
            1
        } else if self.search_dest_with_2_moves() {
            2
        } else {
            -1
        }
    }

    fn search_dest_with_2_moves(&self) -> bool {
        // 足して行けるパターン
        for i in self
            .moves
            .range((Bound::Unbounded, Bound::Excluded(self.mt_len)))
        {
            for j in self.moves.range((
                Bound::Included(self.mt_len - i),
                Bound::Excluded(self.mt_len),
            )) {
                if (i + j) == self.mt_len {
                    return true;
                }
            }
        }

        // 引いて行けるパターン
        for i in self
            .moves
            .range((Bound::Excluded(self.mt_len), Bound::Unbounded))
        {
            for j in self
                .moves
                .range((Bound::Unbounded, Bound::Included(i - self.mt_len)))
            {
                if (i - j) == self.mt_len {
                    return true;
                }
            }
        }

        false
    }
}

fn get_input() -> Input {
    let mut buf = String::new();

    io::stdin().read_line(&mut buf).unwrap();
    let move_len: usize = buf.clone().trim().parse().unwrap();
    buf.clear();

    io::stdin().read_line(&mut buf).unwrap();
    let moves: BTreeSet<usize> = buf
        .clone()
        .trim()
        .splitn(move_len, ' ')
        .map(|s: &str| s.parse().unwrap())
        .collect();
    buf.clear();

    io::stdin().read_line(&mut buf).unwrap();
    let dest: Vec<i64> = buf
        .trim()
        .splitn(2, ' ')
        .map(|s: &str| s.parse().unwrap())
        .collect();

    Input {
        move_len,
        moves,
        dest: (dest[0], dest[1]),
        mt_len: (dest[0].abs() + dest[1].abs()) as usize,
    }
}

fn main() {
    let input = get_input();
    println!("{}", input.search_dest());
}
0