結果
| 問題 | No.1572 XI |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-08-29 11:30:49 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 396 ms / 2,000 ms |
| コード長 | 2,301 bytes |
| コンパイル時間 | 15,131 ms |
| コンパイル使用メモリ | 378,764 KB |
| 実行使用メモリ | 53,504 KB |
| 最終ジャッジ日時 | 2025-03-27 14:34:16 |
| 合計ジャッジ時間 | 23,168 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 45 |
ソースコード
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 });
}