結果

問題 No.334 門松ゲーム
ユーザー phsplsphspls
提出日時 2020-06-27 21:00:23
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,494 bytes
コンパイル時間 5,181 ms
コンパイル使用メモリ 142,888 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 09:52:02
合計ジャッジ時間 1,879 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn is_kadomatsu(i: usize, j: usize, k: usize, val: &Vec<usize>) -> bool {
       (val[i] < val[j] && val[k] < val[j])
    || (val[i] > val[j] && val[k] > val[j])
}

fn dfs(n: usize, val: &Vec<usize>, used: &mut Vec<bool>) -> Option<(usize, usize, usize)> {
    for i in 0..n {
        if used[i] { continue; }
        for j in i..n {
            if used[j] { continue; }
            for k in j..n {
                if used[k] { continue; }
                if is_kadomatsu(i, j, k, val) {
                    used[i] = true;
                    used[j] = true;
                    used[k] = true;
                    let result = dfs(n, val, used);
                    used[i] = false;
                    used[j] = false;
                    used[k] = false;
                    if result.is_none() {
                        return Some((i, j, k));
                    }
                }
            }
        }
    }
    None
}

//TODO
fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut val = String::new();
    std::io::stdin().read_line(&mut val).ok();
    let val: Vec<usize> = val.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let mut used: Vec<bool> = vec![false; n];
    let result = dfs(n, &val, &mut used);
    if result.is_some() {
        println!("{} {} {}", result.unwrap().0, result.unwrap().1, result.unwrap().2);
    } else {
        println!("-1");
    }
}
0