Skip to content

Commit f204639

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

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
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();

test-with-generated-opts.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ meta = { using = ["itertools", "num"] }
139139
name = "ABC151: D - Maze Master"
140140
url = "https://atcoder.jp/contests/abc151/tasks/abc151_d"
141141
matching = "Words"
142-
meta = { using = ["ndarray", "smallvec"] }
142+
meta = { using = ["itertools", "ndarray", "smallvec"] }
143143

144144
[examples.apg4b-a]
145145
name = "APG4b: A - 1.00.はじめに"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy