結果

問題 No.26 シャッフルゲーム
ユーザー yo-kondoyo-kondo
提出日時 2019-01-02 10:20:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 2,414 bytes
コンパイル時間 14,955 ms
コンパイル使用メモリ 378,988 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:03:40
合計ジャッジ時間 14,729 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `shuffle_count` is never read
  --> src/main.rs:15:5
   |
11 | struct InputData {
   |        --------- field in this struct
...
15 |     shuffle_count: i32,
   |     ^^^^^^^^^^^^^
   |
   = note: `InputData` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

//! No.26 シャッフルゲーム
//! https://yukicoder.me/problems/no/26

use std::io;

#[cfg(test)]
mod test;

/// 入力データ
#[derive(Debug)]
struct InputData {
    /// 最初に○印が付いているカップの位置
    first_position: i32,
    /// シャッフル回数
    shuffle_count: i32,
    /// シャッフルした位置
    shuffle_indices: Vec<[i32; 2]>,
}

/// エントリポイント
fn main() {
    let input = input_data();
    println!("{}", position_number(input));
}

/// ○印がついているカップの位置を返します。
fn position_number(input: InputData) -> i32 {
    // カップの位置情報(true:○、false:×)
    let mut correct_answer: [bool; 3] = [false, false, false];

    // 初期値
    correct_answer[(input.first_position - 1) as usize] = true;

    for index in input.shuffle_indices {
        let i = (index[0] - 1) as usize;
        let j = (index[1] - 1) as usize;

        let temp = correct_answer[i];
        correct_answer[i] = correct_answer[j];
        correct_answer[j] = temp;

        // 配列のswapは&mutでムーブされてしまうため、エラーが発生する。
        // error[E0499]: cannot borrow `correct_answer[..]` as mutable more than once at a time
        // std::mem::swap(&mut correct_answer[i], &mut correct_answer[j]);
    }

    // 結果を探す
    for (i, ans) in correct_answer.iter().enumerate() {
        if *ans {
            return i as i32 + 1i32;
        }
    }

    // ここには来ない
    0
}

/// 標準入力から文字列を取得します。
fn input_data() -> InputData {
    // 1行目
    let mut s1 = String::new();
    io::stdin().read_line(&mut s1).unwrap();
    let first_position: i32 = s1.trim().parse().unwrap();

    // 2行目
    let mut s2 = String::new();
    io::stdin().read_line(&mut s2).unwrap();
    let shuffle_count: i32 = s2.trim().parse().unwrap();

    // 3行目以降
    let mut shuffle_indices = Vec::new();
    for _i in 0..shuffle_count {
        let mut s3 = String::new();
        io::stdin().read_line(&mut s3).unwrap();

        let sp: Vec<&str> = s3.trim().split_whitespace().collect();
        let array: [i32; 2] = [
            sp[0].parse().unwrap(),
            sp[1].parse().unwrap(),
        ];
        shuffle_indices.push(array);
    }

    InputData {
        first_position,
        shuffle_count,
        shuffle_indices,
    }
}
0