Submission #43705077


Source Code Expand

use std::collections::VecDeque;

use proconio::{input, marker::Chars};

fn main() {
    input! {
        h: usize,
        w: usize,
        s: [Chars; h],
    };

    let f = |start: (usize, usize)| -> Vec<Vec<usize>> {
        let mut dist = vec![vec![h * w; w]; h];
        let mut deque = VecDeque::new();
        dist[start.0][start.1] = 0;
        deque.push_back(start);
        while let Some((i, j)) = deque.pop_front() {
            let dir = vec![(-1, 0), (0, -1), (0, 1), (1, 0)];
            for (dr, dc) in dir {
                let (nr, nc) = (i as i64 + dr, j as i64 + dc);
                if !(0..h as i64).contains(&nr) || !(0..w as i64).contains(&nc) {
                    continue;
                }
                let (nr, nc) = (nr as usize, nc as usize);
                if s[nr][nc] == '#' {
                    continue;
                }
                let nd = dist[i][j] + 1;
                if dist[nr][nc] <= nd {
                    continue;
                }
                dist[nr][nc] = nd;
                deque.push_back((nr, nc));
            }
        }
        dist
    };

    let g = |dist: &[Vec<usize>]| -> ((usize, usize), usize) {
        let mut pos = (0_usize, 0_usize);
        let mut max = 0_usize;
        for i in (0..h).rev() {
            for j in (0..w).rev() {
                if dist[i][j] == h * w {
                    continue;
                }
                if dist[i][j] > max {
                    max = dist[i][j];
                    pos = (i, j);
                }
            }
        }
        (pos, max)
    };

    let mut start = (0, 0);
    for i in 0..h {
        for j in 0..w {
            if s[i][j] == '.' {
                start = (i, j);
                break;
            }
        }
    }
    let dist1 = f(start);
    let (start, _) = g(&dist1);
    let dist2 = f(start);
    let (_, ans) = g(&dist2);

    println!("{}", ans);
}

Submission Info

Submission Time
Task D - Maze Master
User bouzuya
Language Rust (1.42.0)
Score 400
Code Size 1920 Byte
Status AC
Exec Time 5 ms
Memory 2188 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 16
Set Name Test Cases
Sample sample_01, sample_02
All hand_01, hand_02, hand_03, hand_04, hand_05, hand_06, hand_07, hand_08, hand_09, hand_10, sample_01, sample_02, small_01, small_02, small_03, small_04
Case Name Status Exec Time Memory
hand_01 AC 5 ms 2052 KiB
hand_02 AC 2 ms 2056 KiB
hand_03 AC 1 ms 1980 KiB
hand_04 AC 2 ms 2056 KiB
hand_05 AC 1 ms 2084 KiB
hand_06 AC 2 ms 2188 KiB
hand_07 AC 1 ms 2084 KiB
hand_08 AC 1 ms 2172 KiB
hand_09 AC 3 ms 2068 KiB
hand_10 AC 2 ms 2120 KiB
sample_01 AC 1 ms 2072 KiB
sample_02 AC 2 ms 2048 KiB
small_01 AC 1 ms 1944 KiB
small_02 AC 1 ms 2136 KiB
small_03 AC 1 ms 2040 KiB
small_04 AC 1 ms 2020 KiB