結果

問題 No.20 砂漠のオアシス
ユーザー tonyu0tonyu0
提出日時 2019-12-11 00:21:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 2,147 bytes
コンパイル時間 5,081 ms
コンパイル使用メモリ 156,784 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 11:46:11
合計ジャッジ時間 2,977 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::io::Read;

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let v: i32 = itr.next().unwrap().parse().unwrap();
    let mut ox: usize = itr.next().unwrap().parse().unwrap();
    let mut oy: usize = itr.next().unwrap().parse().unwrap();

    let field: Vec<Vec<i32>> = (0..n)
        .map(|_| {
            (0..n)
                .map(|_| itr.next().unwrap().parse().unwrap())
                .collect()
        })
        .collect();

    const INF: i32 = 40000;
    let mut q = std::collections::BinaryHeap::new();
    let mut first: Vec<Vec<i32>> = vec![vec![-INF; n]; n];
    let mut second: Vec<Vec<i32>> = vec![vec![-INF; n]; n];

    q.push((v, 0));
    first[0][0] = v;
    while let Some((life, pos)) = q.pop() {
        let y = pos / 1000;
        let x = pos % 1000;
        for (dy, dx) in vec![(-1, 0), (0, -1), (1, 0), (0, 1)] {
            let ny = (y as isize + dy) as usize;
            let nx = (x as isize + dx) as usize;
            if ny < n && nx < n && first[ny][nx] == -INF {
                first[ny][nx] = life - field[ny][nx];
                q.push((first[ny][nx], ny * 1000 + nx));
            }
        }
    }
    if ox != 0 || oy != 0 {
        oy -= 1;
        ox -= 1;
        second[oy][ox] = first[oy][ox] * 2;
        q.push((second[oy][ox], oy * 1000 + ox));
        while let Some((life, pos)) = q.pop() {
            let y = pos / 1000;
            let x = pos % 1000;
            for (dy, dx) in vec![(-1, 0), (0, -1), (1, 0), (0, 1)] {
                let ny = (y as isize + dy) as usize;
                let nx = (x as isize + dx) as usize;
                if ny < n && nx < n && second[ny][nx] == -INF {
                    second[ny][nx] = life - field[ny][nx];
                    q.push((second[ny][nx], ny * 1000 + nx));
                }
            }
        }
    }
    println!(
        "{}",
        if first[n - 1][n - 1] > 0 || second[n - 1][n - 1] > 0 {
            "YES"
        } else {
            "NO"
        }
    );
}
0