Skip to content

Commit 4ae0c3c

Browse files
committed
Modify abc151-d
1 parent 180ad35 commit 4ae0c3c

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

examples/abc151-d.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// https://atcoder.jp/contests/abc151/tasks/abc151_d
22

33
use ndarray::Array;
4-
use smallvec::{smallvec, SmallVec};
4+
use smallvec::SmallVec;
55

6-
use std::collections::VecDeque;
76
use std::io::{self, Read};
8-
use std::iter;
7+
use std::{iter, mem};
98

109
fn main() {
1110
let mut input = read_to_static(io::stdin()).split_whitespace();
@@ -17,28 +16,28 @@ fn main() {
1716
({ Maze<$c:literal, ($h:expr, $w:expr)> }) => {
1817
Array::from_shape_vec(
1918
($h, $w),
20-
(0..$h)
21-
.fold(vec![], |mut acc, _| {
22-
acc.extend(input.next().unwrap().bytes().map(|c| c == $c));
23-
acc
24-
}),
19+
itertools::concat((0..$h).map(|_| read!({ Row<$c> }))),
2520
)
2621
.unwrap()
2722
};
23+
({ Row<$c:literal> }) => {
24+
read!({ Bytes }).into_iter().map(|c| c == $c).collect::<Vec<_>>()
25+
};
26+
({ Bytes }) => {
27+
read!(String).into_bytes()
28+
};
2829
}
2930

3031
let (h, w) = read!((usize, usize));
3132
let maze = read!({ Maze<b'.', (h, w)> });
3233

33-
let neighbors = Array::from_shape_fn((h, w), |(i, j)| -> SmallVec<[_; 4]> {
34-
let mut neighbors = smallvec![];
35-
macro_rules! push {
36-
(if $cond:expr => $pos:expr) => {
37-
if $cond && maze[$pos] {
38-
neighbors.push($pos);
39-
}
40-
};
41-
}
34+
let neighbors = Array::from_shape_fn((h, w), |(i, j)| {
35+
let mut neighbors = SmallVec::<[_; 4]>::new();
36+
macro_rules! push((if $cond:expr => $pos:expr) => {
37+
if $cond && maze[$pos] {
38+
neighbors.push($pos);
39+
}
40+
});
4241
push!(if 0 < i => (i - 1, j));
4342
push!(if i < h - 1 => (i + 1, j));
4443
push!(if 0 < j => (i, j - 1));
@@ -50,21 +49,21 @@ fn main() {
5049
.flat_map(|i| (0..w).map(move |j| (i, j)))
5150
.filter(|&p| maze[p])
5251
.map(|start| {
53-
let mut longest = 0;
54-
let mut queue = iter::once((start, 0)).collect::<VecDeque<_>>();
52+
let mut queue = vec![start];
5553
let mut unvisited = maze.clone();
5654
unvisited[start] = false;
5755

58-
while let Some((pos, dist)) = queue.pop_front() {
59-
for &neighbor in &neighbors[pos] {
60-
if unvisited[neighbor] {
61-
unvisited[neighbor] = false;
62-
longest = dist + 1;
63-
queue.push_back((neighbor, longest));
64-
}
65-
}
66-
}
67-
longest
56+
iter::repeat(())
57+
.take_while(|_| {
58+
queue = queue
59+
.iter()
60+
.flat_map(|&p| &neighbors[p])
61+
.copied()
62+
.filter(|&p| mem::replace(&mut unvisited[p], false))
63+
.collect();
64+
!queue.is_empty()
65+
})
66+
.count()
6867
})
6968
.max()
7069
.unwrap();

0 commit comments

Comments
 (0)