-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathabc151-d.rs
58 lines (51 loc) · 1.55 KB
/
abc151-d.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// https://atcoder.jp/contests/abc151/tasks/abc151_d
use ndarray::Array;
use proconio::input;
use proconio::marker::Bytes;
use smallvec::SmallVec;
use std::{iter, mem};
fn main() {
input! {
h: usize,
w: usize,
sss: [Bytes; h],
}
let maze = Array::from_shape_vec((h, w), itertools::concat(sss))
.unwrap()
.map(|&c| c == b'.');
let neighbors = Array::from_shape_fn((h, w), |(i, j)| {
let mut neighbors = SmallVec::<[_; 4]>::new();
macro_rules! push((if $cond:expr => $pos:expr) => {
if $cond && maze[$pos] {
neighbors.push($pos);
}
});
push!(if 0 < i => (i - 1, j));
push!(if i < h - 1 => (i + 1, j));
push!(if 0 < j => (i, j - 1));
push!(if j < w - 1 => (i, j + 1));
neighbors
});
let ans = (0..h)
.flat_map(|i| (0..w).map(move |j| (i, j)))
.filter(|&p| maze[p])
.map(|start| {
let mut queue = vec![start];
let mut unvisited = maze.clone();
unvisited[start] = false;
iter::repeat(())
.take_while(|_| {
queue = queue
.iter()
.flat_map(|&p| &neighbors[p])
.copied()
.filter(|&p| mem::take(&mut unvisited[p]))
.collect();
!queue.is_empty()
})
.count()
})
.max()
.unwrap();
println!("{}", ans);
}