結果

問題 No.2328 Build Walls
ユーザー so-hey
提出日時 2023-06-10 00:30:25
言語 Rust
(1.83.0 + proconio)
結果
RE  
実行時間 -
コード長 1,426 bytes
コンパイル時間 10,993 ms
コンパイル使用メモリ 404,924 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2025-01-02 07:04:50
合計ジャッジ時間 13,102 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 15 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 dp[i][j] != INF {
                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