Skip to content

Commit 10a178a

Browse files
committed
Modify abc151-d
1 parent 180ad35 commit 10a178a

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

examples/abc151-d.rs

Lines changed: 21 additions & 24 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();
@@ -30,15 +29,13 @@ fn main() {
3029
let (h, w) = read!((usize, usize));
3130
let maze = read!({ Maze<b'.', (h, w)> });
3231

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-
}
32+
let neighbors = Array::from_shape_fn((h, w), |(i, j)| {
33+
let mut neighbors = SmallVec::<[_; 4]>::new();
34+
macro_rules! push((if $cond:expr => $pos:expr) => {
35+
if $cond && maze[$pos] {
36+
neighbors.push($pos);
37+
}
38+
});
4239
push!(if 0 < i => (i - 1, j));
4340
push!(if i < h - 1 => (i + 1, j));
4441
push!(if 0 < j => (i, j - 1));
@@ -50,21 +47,21 @@ fn main() {
5047
.flat_map(|i| (0..w).map(move |j| (i, j)))
5148
.filter(|&p| maze[p])
5249
.map(|start| {
53-
let mut longest = 0;
54-
let mut queue = iter::once((start, 0)).collect::<VecDeque<_>>();
50+
let mut queue = vec![start];
5551
let mut unvisited = maze.clone();
5652
unvisited[start] = false;
5753

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
54+
iter::repeat(())
55+
.take_while(|_| {
56+
queue = queue
57+
.iter()
58+
.flat_map(|&p| &neighbors[p])
59+
.copied()
60+
.filter(|&p| mem::replace(&mut unvisited[p], false))
61+
.collect();
62+
!queue.is_empty()
63+
})
64+
.count()
6865
})
6966
.max()
7067
.unwrap();

0 commit comments

Comments
 (0)