結果

問題 No.1572 XI
ユーザー koba-e964koba-e964
提出日時 2023-08-29 11:30:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 2,301 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 154,632 KB
実行使用メモリ 53,348 KB
最終ジャッジ日時 2023-08-29 11:31:03
合計ジャッジ時間 13,507 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 78 ms
12,824 KB
testcase_05 AC 182 ms
27,032 KB
testcase_06 AC 220 ms
31,956 KB
testcase_07 AC 6 ms
4,384 KB
testcase_08 AC 188 ms
27,772 KB
testcase_09 AC 87 ms
14,200 KB
testcase_10 AC 145 ms
22,136 KB
testcase_11 AC 12 ms
4,380 KB
testcase_12 AC 63 ms
10,844 KB
testcase_13 AC 15 ms
4,380 KB
testcase_14 AC 65 ms
11,120 KB
testcase_15 AC 15 ms
4,384 KB
testcase_16 AC 52 ms
9,404 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 79 ms
12,856 KB
testcase_19 AC 195 ms
28,856 KB
testcase_20 AC 125 ms
19,288 KB
testcase_21 AC 230 ms
33,200 KB
testcase_22 AC 177 ms
26,232 KB
testcase_23 AC 4 ms
4,384 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 1 ms
4,384 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 351 ms
48,608 KB
testcase_35 AC 339 ms
48,148 KB
testcase_36 AC 352 ms
49,396 KB
testcase_37 AC 371 ms
51,160 KB
testcase_38 AC 351 ms
50,328 KB
testcase_39 AC 358 ms
50,716 KB
testcase_40 AC 337 ms
47,400 KB
testcase_41 AC 359 ms
49,732 KB
testcase_42 AC 355 ms
49,824 KB
testcase_43 AC 349 ms
49,144 KB
testcase_44 AC 381 ms
53,348 KB
testcase_45 AC 384 ms
53,344 KB
testcase_46 AC 376 ms
53,348 KB
testcase_47 AC 51 ms
52,832 KB
testcase_48 AC 371 ms
53,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    input! {
        h: usize, w: usize,
        sx: usize1, sy: usize1,
        gx: usize1, gy: usize1,
        a: [chars; h],
    }
    const INF: i64 = 1 << 28;
    let mut dist = vec![vec![[INF; 6]; w]; h];
    let mut que = VecDeque::new();
    que.push_back((0, sx, sy, 4));
    let dxy = [(0i32, 1i32), (-1, 0), (0, -1), (1, 0)];
    while let Some((d, x, y, z)) = que.pop_front() {
        if dist[x][y][z] <= d { continue; }
        dist[x][y][z] = d;
        for i in 0..4 {
            let nz = if z == 4 {
                i
            } else if z == 5 {
                (i + 2) % 4
            } else if z == i {
                5
            } else if z == (i + 2) % 4 {
                4
            } else {
                z
            };
            let (dx, dy) = dxy[i];
            let nx = (x as i32 + dx) as usize;
            let ny = (y as i32 + dy) as usize;
            if nx >= h || ny >= w {
                continue;
            }
            if a[nx][ny] == '#' { continue; }
            que.push_back((d + 1, nx, ny, nz));
        }
    }
    let ans = dist[gx][gy][4];
    println!("{}", if ans >= INF { -1 } else { ans });
}
0