https://atcoder.jp/contests/abc258/tasks/abc258_b
違う型を掛けるにはこうします。
impl Mul<i32> for Point { type Output = Self; fn mul(self, c: i32) -> Self { Self { x: self.x * c, y: self.y * c } } }
いちばん大きい数字のマスから辿っていますが、あまり意味は無いですね。本当は各桁で一番大きい数字以上でなければ捨てるような処理を行わないといけないと思います。
あと、Iteratorを返す関数を作るのは難しそうですね。
// Number Box #![allow(non_snake_case)] use std::ops::Add; use std::ops::Mul; //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_matrix() -> Vec<Vec<i32>> { fn read_row() -> Vec<i32> { read::<String>().chars().map(|c| (c as i32) - 48).collect() } let N: i32 = read(); (0..N).map(|_| read_row()).collect() } //////////////////// Point //////////////////// #[derive(Clone, Copy)] struct Point { x: i32, y: i32, } impl Add for Point { type Output = Self; fn add(self, other: Self) -> Self { Self { x: self.x + other.x, y: self.y + other.y } } } impl Mul<i32> for Point { type Output = Self; fn mul(self, c: i32) -> Self { Self { x: self.x * c, y: self.y * c } } } //////////////////// process //////////////////// fn make_dirs() -> Vec<Point> { (0..9).filter(|&i| i != 4).map(|i| Point { x: i/3-1, y: i%3-1 }).collect() } fn mod_point(pt: Point, d: i32) -> Point { Point { x: (pt.x + d) % d, y: (pt.y + d) % d } } fn make_number(A: &Vec<Vec<i32>>, pt: &Point, dir: &Point) -> i64 { let M = A.len() as i32; let points = (0..M).map(|i| mod_point(*pt + *dir * i, M)); points.fold(0, |x, y| x * 10 + (A[y.x as usize][y.y as usize] as i64)) } fn max_number(A: &Vec<Vec<i32>>, pt: &Point, dirs: &Vec<Point>) -> i64 { dirs.iter().map(|dir| make_number(A, pt, dir)).max().unwrap() } fn main() { let A = read_matrix(); let N = A.len(); let max_digit = A.iter().map(|v| v.iter().max().unwrap()).max().unwrap(); let dirs = make_dirs(); let points = (0..(N*N)).map(|i| (i/N, i%N)). filter(|(x, y)| A[*x][*y] == *max_digit). map(|(x, y)| Point { x: x as i32, y: y as i32 }); let max_n = points.map(|pt| max_number(&A, &pt, &dirs)).max().unwrap(); println!("{}", max_n) }