Submission #71690388


Source Code Expand

use std::collections::{HashSet, VecDeque};

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

fn get_positions(a: &[Vec<char>], c: char) -> Vec<(usize, usize)> {
    let mut v = vec![];
    for (i, a) in a.iter().enumerate() {
        for (j, &x) in a.iter().enumerate() {
            if x == c {
                v.push((i, j));
            }
        }
    }
    v
}

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

    let dirs = [(0, -1), (0, 1), (-1, 0), (1, 0)];

    let mut visited = vec![vec![-1; w]; h];
    let mut warped = HashSet::new();

    let mut queue = VecDeque::new();
    queue.push_back((0, (0, 0)));
    visited[0][0] = 0;

    while let Some((count, (i, j))) = queue.pop_front() {
        if i == h - 1 && j == w - 1 {
            println!("{count}");
            return;
        }

        for &(di, dj) in &dirs {
            let i0 = i as i64 + di;
            let j0 = j as i64 + dj;
            if i0 < 0 || j0 < 0 {
                continue;
            }
            let i0 = i0 as usize;
            let j0 = j0 as usize;
            if i0 >= h || j0 >= w {
                continue;
            }

            let c = s[i0][j0];
            if c == '#' {
                continue;
            }
            if visited[i0][j0] != -1 {
                continue;
            }
            visited[i0][j0] = count;
            queue.push_back((count + 1, (i0, j0)));
        }

        let c = s[i][j];
        if 'a' <= c && c <= 'z' && warped.insert(c) {
            let v = get_positions(&s, c);
            for (i0, j0) in v {
                if visited[i0][j0] != -1 {
                    continue;
                }
                visited[i0][j0] = count;
                queue.push_back((count + 1, (i0, j0)));
            }
        }
    }

    println!("-1");
}

Submission Info

Submission Time
Task D - Teleport Maze
User hossie
Language Rust (rustc 1.89.0)
Score 400
Code Size 1831 Byte
Status AC
Exec Time 60 ms
Memory 49016 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 51
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 1920 KiB
00_sample_01.txt AC 1 ms 1920 KiB
00_sample_02.txt AC 1 ms 1888 KiB
00_sample_03.txt AC 1 ms 2088 KiB
01_random_00.txt AC 1 ms 2096 KiB
01_random_01.txt AC 14 ms 11548 KiB
01_random_02.txt AC 23 ms 9840 KiB
01_random_03.txt AC 60 ms 33984 KiB
01_random_04.txt AC 34 ms 10796 KiB
01_random_05.txt AC 24 ms 16120 KiB
01_random_06.txt AC 39 ms 13172 KiB
01_random_07.txt AC 40 ms 10884 KiB
01_random_08.txt AC 1 ms 2196 KiB
01_random_09.txt AC 13 ms 4996 KiB
01_random_10.txt AC 29 ms 9976 KiB
01_random_11.txt AC 53 ms 29176 KiB
01_random_12.txt AC 36 ms 11560 KiB
01_random_13.txt AC 25 ms 11696 KiB
01_random_14.txt AC 46 ms 13044 KiB
01_random_15.txt AC 43 ms 10856 KiB
01_random_16.txt AC 1 ms 2132 KiB
01_random_17.txt AC 6 ms 3480 KiB
01_random_18.txt AC 5 ms 9900 KiB
01_random_19.txt AC 43 ms 24424 KiB
01_random_20.txt AC 26 ms 17728 KiB
01_random_21.txt AC 34 ms 21152 KiB
01_random_22.txt AC 5 ms 9884 KiB
01_random_23.txt AC 22 ms 14960 KiB
01_random_24.txt AC 1 ms 2152 KiB
01_random_25.txt AC 3 ms 3304 KiB
01_random_26.txt AC 5 ms 9940 KiB
01_random_27.txt AC 32 ms 19556 KiB
01_random_28.txt AC 37 ms 19052 KiB
01_random_29.txt AC 29 ms 16120 KiB
01_random_30.txt AC 27 ms 16808 KiB
01_random_31.txt AC 30 ms 17320 KiB
01_random_32.txt AC 1 ms 2020 KiB
01_random_33.txt AC 9 ms 6028 KiB
01_random_34.txt AC 5 ms 9864 KiB
01_random_35.txt AC 26 ms 14248 KiB
01_random_36.txt AC 6 ms 10008 KiB
01_random_37.txt AC 18 ms 10752 KiB
01_random_38.txt AC 28 ms 13280 KiB
01_random_39.txt AC 19 ms 12696 KiB
02_handmade_00.txt AC 22 ms 10096 KiB
02_handmade_01.txt AC 36 ms 49016 KiB
02_handmade_02.txt AC 6 ms 9896 KiB
02_handmade_03.txt AC 6 ms 10008 KiB
02_handmade_04.txt AC 9 ms 9948 KiB
02_handmade_05.txt AC 29 ms 9852 KiB
02_handmade_06.txt AC 49 ms 14024 KiB