結果

問題 No.1572 XI
ユーザー akakimidoriakakimidori
提出日時 2021-11-16 12:45:19
言語 Rust
(1.77.0)
結果
AC  
実行時間 342 ms / 2,000 ms
コード長 4,058 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 159,824 KB
実行使用メモリ 29,008 KB
最終ジャッジ日時 2023-08-22 12:08:56
合計ジャッジ時間 11,442 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 66 ms
7,792 KB
testcase_05 AC 158 ms
15,140 KB
testcase_06 AC 187 ms
18,228 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 159 ms
15,552 KB
testcase_09 AC 73 ms
8,576 KB
testcase_10 AC 121 ms
12,608 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 51 ms
6,636 KB
testcase_13 AC 12 ms
4,380 KB
testcase_14 AC 53 ms
6,940 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 42 ms
6,096 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 65 ms
7,720 KB
testcase_19 AC 165 ms
16,196 KB
testcase_20 AC 105 ms
11,372 KB
testcase_21 AC 200 ms
18,848 KB
testcase_22 AC 152 ms
14,828 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 306 ms
26,624 KB
testcase_35 AC 300 ms
26,260 KB
testcase_36 AC 303 ms
26,904 KB
testcase_37 AC 320 ms
27,892 KB
testcase_38 AC 307 ms
27,384 KB
testcase_39 AC 324 ms
27,608 KB
testcase_40 AC 297 ms
25,628 KB
testcase_41 AC 311 ms
26,820 KB
testcase_42 AC 309 ms
27,196 KB
testcase_43 AC 304 ms
26,804 KB
testcase_44 AC 336 ms
29,004 KB
testcase_45 AC 342 ms
29,004 KB
testcase_46 AC 328 ms
28,988 KB
testcase_47 AC 23 ms
28,740 KB
testcase_48 AC 325 ms
29,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

use std::collections::*;

type Deque<T> = VecDeque<T>;

fn run() {
    input! {
        h: usize,
        w: usize,
        src: (usize, usize),
        dst: (usize, usize),
        a: [bytes; h],
    }
    let mut a = a;
    for a in a.iter_mut() {
        a.insert(0, b'#');
        a.push(b'#');
    }
    a.insert(0, vec![b'#'; w + 2]);
    a.push(vec![b'#'; w + 2]);
    type Dice = [u8; 6];
    fn rot(dice: &mut Dice, map: [usize; 6]) {
        let mut next = Dice::default();
        for (dice, map) in dice.iter().zip(&map) {
            next[*map] = *dice;
        }
        *dice = next;
    }
    fn left(x: usize) -> usize {
        let mut dice = [0; 6];
        dice[x] = 1;
        rot(&mut dice, [3, 0, 1, 2, 4, 5]);
        dice.iter().position(|d| *d == 1).unwrap()
    }
    fn right(x: usize) -> usize {
        let mut dice = [0; 6];
        dice[x] = 1;
        rot(&mut dice, [1, 2, 3, 0, 4, 5]);
        dice.iter().position(|d| *d == 1).unwrap()
    }
    fn up(x: usize) -> usize {
        let mut dice = [0; 6];
        dice[x] = 1;
        rot(&mut dice, [4, 1, 5, 3, 2, 0]);
        dice.iter().position(|d| *d == 1).unwrap()
    }
    fn down(x: usize) -> usize {
        let mut dice = [0; 6];
        dice[x] = 1;
        rot(&mut dice, [5, 1, 4, 3, 0, 2]);
        dice.iter().position(|d| *d == 1).unwrap()
    }
    let inf = 1000 * 1000 * 6i32;
    let mut dp = vec![vec![vec![inf; w + 2]; h + 2]; 6];
    dp[0][src.0][src.1] = 0;
    let mut deq = Deque::new();
    deq.push_back((0, src.0, src.1));
    while let Some((k, x, y)) = deq.pop_front() {
        let d = dp[k][x][y] + 1;
        let mut test = |dx: usize, dy: usize, f: &dyn Fn(usize) -> usize| {
            let x = x + dx;
            let y = y + dy;
            let k = (*f)(k);
            if a[x][y] == b'.' && dp[k][x][y].chmin(d) {
                deq.push_back((k, x, y));
            }
        };
        test(1, 0, &right);
        test(0, 1, &up);
        test(!0, 0, &left);
        test(0, !0, &down);
    }
    let mut ans = dp[0][dst.0][dst.1];
    if ans == inf {
        ans = -1;
    }
    println!("{}", ans);
}

fn main() {
    run();
}
0