結果
問題 | No.1572 XI |
ユーザー | akakimidori |
提出日時 | 2021-11-16 12:45:19 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 366 ms / 2,000 ms |
コード長 | 4,058 bytes |
コンパイル時間 | 12,863 ms |
コンパイル使用メモリ | 400,572 KB |
実行使用メモリ | 28,928 KB |
最終ジャッジ日時 | 2024-05-09 17:51:01 |
合計ジャッジ時間 | 22,123 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 71 ms
7,680 KB |
testcase_05 | AC | 168 ms
14,848 KB |
testcase_06 | AC | 196 ms
17,664 KB |
testcase_07 | AC | 6 ms
5,376 KB |
testcase_08 | AC | 178 ms
15,360 KB |
testcase_09 | AC | 76 ms
8,576 KB |
testcase_10 | AC | 127 ms
12,544 KB |
testcase_11 | AC | 11 ms
5,376 KB |
testcase_12 | AC | 53 ms
6,656 KB |
testcase_13 | AC | 13 ms
5,376 KB |
testcase_14 | AC | 57 ms
6,656 KB |
testcase_15 | AC | 14 ms
5,376 KB |
testcase_16 | AC | 44 ms
5,760 KB |
testcase_17 | AC | 5 ms
5,376 KB |
testcase_18 | AC | 72 ms
7,680 KB |
testcase_19 | AC | 168 ms
16,000 KB |
testcase_20 | AC | 108 ms
11,008 KB |
testcase_21 | AC | 207 ms
18,304 KB |
testcase_22 | AC | 158 ms
14,592 KB |
testcase_23 | AC | 4 ms
5,376 KB |
testcase_24 | AC | 2 ms
5,376 KB |
testcase_25 | AC | 1 ms
5,376 KB |
testcase_26 | AC | 1 ms
5,376 KB |
testcase_27 | AC | 1 ms
5,376 KB |
testcase_28 | AC | 1 ms
5,376 KB |
testcase_29 | AC | 1 ms
5,376 KB |
testcase_30 | AC | 1 ms
5,376 KB |
testcase_31 | AC | 1 ms
5,376 KB |
testcase_32 | AC | 1 ms
5,376 KB |
testcase_33 | AC | 1 ms
5,376 KB |
testcase_34 | AC | 328 ms
26,368 KB |
testcase_35 | AC | 329 ms
26,112 KB |
testcase_36 | AC | 323 ms
26,880 KB |
testcase_37 | AC | 352 ms
27,904 KB |
testcase_38 | AC | 323 ms
27,136 KB |
testcase_39 | AC | 342 ms
27,392 KB |
testcase_40 | AC | 318 ms
25,472 KB |
testcase_41 | AC | 330 ms
26,624 KB |
testcase_42 | AC | 339 ms
27,136 KB |
testcase_43 | AC | 332 ms
26,624 KB |
testcase_44 | AC | 361 ms
28,928 KB |
testcase_45 | AC | 366 ms
28,928 KB |
testcase_46 | AC | 350 ms
28,800 KB |
testcase_47 | AC | 24 ms
28,672 KB |
testcase_48 | AC | 345 ms
28,928 KB |
ソースコード
// ---------- 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(); }