結果

問題 No.2328 Build Walls
ユーザー so-heyso-hey
提出日時 2023-06-10 01:30:38
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,547 bytes
コンパイル時間 3,864 ms
コンパイル使用メモリ 152,196 KB
実行使用メモリ 10,280 KB
最終ジャッジ日時 2023-08-30 16:06:29
合計ジャッジ時間 10,882 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner  {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

use std::{cmp::Reverse, collections::*};

use crate::scanner::Scanner;

const INF: isize = 1e18 as isize;

const DIJ: [(usize, usize); 8] = [
    (!0, !0),
    (!0, 0),
    (!0, 1),
    (0, !0),
    (0, 1),
    (1, !0),
    (1, 0),
    (1, 1)
];

fn main() {
    let mut scanner = Scanner::new();
    let t = 1;
    for _ in 0..t {
        solve(&mut scanner);
    }
}

fn solve(scanner: &mut Scanner) {
    let h: usize = scanner.next();
    let w: usize = scanner.next();
    let h = h - 2;
    let mut grid = vec![vec![-1 as isize; w]; h];
    let mut wall = vec![vec![false; w]; h];
    for i in 0..h {
        for j in 0..w {
            grid[i][j] = scanner.next();
            wall[i][j] = grid[i][j] == -1;
        }
    }
    let mut dist = vec![vec![INF; w]; h];
    let mut que = BinaryHeap::new();
    for i in 0..h {
        if !wall[i][0] {
            dist[i][0] = grid[i][0];
            que.push(Reverse((grid[i][0], i, 0)));
        }
    }
    while let Some(Reverse((cd, ci, cj))) = que.pop() {
        if dist[ci][cj] < cd { continue }
        for &(di, dj) in DIJ.iter() {
            let ni = ci + di;
            let nj = cj + dj;
            if !(ni < h && nj < w && wall[ni][nj]) { continue }
            let nd = cd + grid[ni][nj];
            if dist[ni][nj] > nd {
                dist[ni][nj] = nd;
                que.push(Reverse((nd, ni, nj)));
            }
        }
    }
    let mut ans = INF;
    for i in 0..h {
        ans = ans.min(dist[i][w-1]);
    }
    if ans == INF {
        println!("{}", -1);
    } else {
        println!("{}", ans);
    }
}
0