Submission #61378796


Source Code Expand

use std::collections::VecDeque;

#[allow(unused_imports)]
use proconio::{input, marker::Chars};

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

    let mut start=(0, 0);
    let mut goal = (0, 0);
    for i in 0..h {
        for j in 0..w {
            if s[i][j] == 'S' {
                start = (i, j);
            }
            if s[i][j] == 'G' {
                goal = (i, j);
            }
        }
    }

    let mut seen = vec![vec![vec![INF; w]; h]; 2];
    seen[0][start.0][start.1] = 0;
    seen[1][start.0][start.1] = 0;

    let mut que = VecDeque::new();
    que.push_back((0, start));
    que.push_back((1, start));


    while let Some((i, (pi, pj))) = que.pop_front() {
        let nxt= 1 - i;
        for r in 0..4 {
            if r % 2 == i {
                continue;
            }

            let ni = pi.wrapping_add(DI[r]);
            let nj = pj.wrapping_add(DJ[r]);
            if ni < h && nj < w && seen[nxt][ni][nj] == INF && s[ni][nj] != '#' {
                seen[nxt][ni][nj] = seen[i][pi][pj] + 1;
                que.push_back((nxt, (ni, nj)));
            }
        }
    }

    let ans = seen[0][goal.0][goal.1].min(seen[1][goal.0][goal.1]);
    println!("{}", if ans == INF { -1 } else { ans as i64 });
}

pub static INF: u64 = 1e18 as u64;
pub static DI: &[usize] = &[0, !0, 0, 1, !0, 1, !0, 1];
pub static DJ: &[usize] = &[!0, 0, 1, 0, !0, !0, 1, 1];
pub static DC: &[char] = &['L', 'U', 'R', 'D'];
pub trait Debuggable {
    fn debug_string(&self) -> String;
}

impl<T: std::fmt::Debug + std::fmt::Display> Debuggable for Vec<T> {
    fn debug_string(&self) -> String {
        use itertools::Itertools;
        use std::iter::repeat;
        if let Some(max_size) = self.iter()
            .enumerate()
            .map(|(i, x)| (format!("{:?}", x).len()).max(format!("{:?}", i).len()))
            .max() {
                let mut idx = String::from("idx |");   
                let mut val = String::from("val |");   
                for (i, xi) in self.iter().enumerate() {
                    idx.push_str(
                        &format!(" {:>w$} |", i, w=max_size)
                    );
                    val.push_str(
                        &format!(" {:>w$} |", xi, w=max_size)
                    );
                }

                format!("{}\n{}\n{}\n", idx, repeat('-').take(idx.len()).join(""), val)
            } else {
                format!("idx | \nval |\n")
            }
    }
}

impl<T: std::fmt::Debug + std::fmt::Display> Debuggable for std::collections::BTreeSet<T> {
    fn debug_string(&self) -> String {
        use itertools::Itertools;
        format!("{{ {} }}", self.iter().join(", "))
    }
}

impl<T, U> Debuggable for std::collections::BTreeMap<T, U> 
where T: std::fmt::Debug + std::fmt::Display, U: std::fmt::Debug + std::fmt::Display
{
    fn debug_string(&self) -> String {
        use itertools::Itertools;
        format!(
            "{{\n{}\n }}", self.iter()
                .map(|(k, v)| format!("{k} -> {v}, "))
                .join("\n")
        )
    }
}

// lg! マクロの定義
#[macro_export]
macro_rules! lg {
    ($val:expr) => {
        #[cfg(feature = "local")]
        {
            {
                use Debuggable;
                let val = &$val;
                eprintln!(
                    "[{}:{}] {} = \n{}",
                    file!(),
                    line!(),
                    stringify!($val),
                    val.debug_string()
                );
                val
            }
        }
    }
}
    

Submission Info

Submission Time
Task D - Snaky Walk
User ardRiriy
Language Rust (rustc 1.70.0)
Score 400
Code Size 3581 Byte
Status AC
Exec Time 55 ms
Memory 22576 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 57
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.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, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 1784 KiB
00_sample_01.txt AC 1 ms 1776 KiB
00_sample_02.txt AC 1 ms 1820 KiB
01_random_00.txt AC 10 ms 8164 KiB
01_random_01.txt AC 1 ms 2300 KiB
01_random_02.txt AC 10 ms 18268 KiB
01_random_03.txt AC 3 ms 6320 KiB
01_random_04.txt AC 7 ms 13436 KiB
01_random_05.txt AC 1 ms 2308 KiB
01_random_06.txt AC 1 ms 1968 KiB
01_random_07.txt AC 1 ms 2016 KiB
01_random_08.txt AC 12 ms 22556 KiB
01_random_09.txt AC 55 ms 22372 KiB
01_random_10.txt AC 53 ms 22576 KiB
01_random_11.txt AC 43 ms 22420 KiB
01_random_12.txt AC 48 ms 22488 KiB
01_random_13.txt AC 44 ms 22504 KiB
01_random_14.txt AC 50 ms 22528 KiB
01_random_15.txt AC 12 ms 22436 KiB
01_random_16.txt AC 12 ms 22504 KiB
01_random_17.txt AC 12 ms 22460 KiB
01_random_18.txt AC 55 ms 22420 KiB
01_random_19.txt AC 50 ms 22516 KiB
02_random2_00.txt AC 12 ms 22420 KiB
02_random2_01.txt AC 12 ms 22436 KiB
02_random2_02.txt AC 12 ms 22504 KiB
02_random2_03.txt AC 12 ms 22480 KiB
02_random2_04.txt AC 12 ms 22560 KiB
02_random2_05.txt AC 13 ms 22416 KiB
02_random2_06.txt AC 14 ms 22456 KiB
02_random2_07.txt AC 12 ms 22408 KiB
02_random2_08.txt AC 12 ms 22500 KiB
02_random2_09.txt AC 13 ms 22372 KiB
02_random2_10.txt AC 12 ms 22404 KiB
02_random2_11.txt AC 11 ms 22488 KiB
02_random2_12.txt AC 11 ms 22496 KiB
02_random2_13.txt AC 11 ms 22564 KiB
02_random2_14.txt AC 12 ms 22468 KiB
02_random2_15.txt AC 12 ms 22432 KiB
02_random2_16.txt AC 11 ms 22540 KiB
02_random2_17.txt AC 11 ms 22404 KiB
02_random2_18.txt AC 11 ms 22480 KiB
02_random2_19.txt AC 13 ms 22436 KiB
03_random3_00.txt AC 34 ms 22476 KiB
03_random3_01.txt AC 24 ms 22528 KiB
03_random3_02.txt AC 38 ms 22472 KiB
03_random3_03.txt AC 46 ms 22416 KiB
04_random4_00.txt AC 32 ms 22416 KiB
04_random4_01.txt AC 30 ms 22552 KiB
04_random4_02.txt AC 32 ms 22524 KiB
04_random4_03.txt AC 31 ms 22352 KiB
05_handmade_00.txt AC 21 ms 22488 KiB
05_handmade_01.txt AC 33 ms 22520 KiB
05_handmade_02.txt AC 1 ms 1984 KiB
05_handmade_03.txt AC 1 ms 1928 KiB
05_handmade_04.txt AC 1 ms 1984 KiB
05_handmade_05.txt AC 37 ms 22520 KiB