結果

問題 No.2328 Build Walls
ユーザー so-heyso-hey
提出日時 2023-06-10 00:28:14
言語 Rust
(1.83.0 + proconio)
結果
RE  
実行時間 -
コード長 1,351 bytes
コンパイル時間 12,989 ms
コンパイル使用メモリ 400,128 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2025-01-02 07:02:05
合計ジャッジ時間 13,818 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 22 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::min;

const INF: isize = 1000000000000000000;
fn main() {
    let (h, w): (usize, usize) = {
        let mut line: String = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap()
        )
    };
    let mut dp = vec![vec![INF; w]; h-2];
    for i in 0..h-2 {
        let a: Vec<isize> = {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|x| x.parse().unwrap())
                .collect()
        };
        for j in 0..w {
            if a[j] != -1 {
                dp[i][j] = a[j];
            }
        }
    }
    for j in 1..w {
        for i in 0..h-2 {
            if i == 0 {
                dp[i][j] += min(dp[i][j-1], dp[i+1][j-1]);
            } else if i == h-3 {
                dp[i][j] += min(dp[i-1][j-1], dp[i][j-1]);
            } else {
                dp[i][j] += min(dp[i][j-1], dp[i-1][j-1].min(dp[i+1][j-1]));
            }
        }
    }
    let mut ans = INF;
    for i in 0..h-2 {
        ans = ans.min(dp[i][w-1]);
    }
    if ans == INF {
        println!("-1");
    } else {
        println!("{}", ans);
    }
}
0