Submission #70798326


Source Code Expand

#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(unused_mut)]
#![allow(unused_variables)]
#![allow(dead_code)]
#![allow(unused_macros)]

use itertools::Itertools;
use proconio::input;
use proconio::marker::{Chars, Usize1};

type Vec2<T> = Vec<Vec<T>>;
type Vec3<T> = Vec<Vec<Vec<T>>>;
#[rustfmt::skip]
macro_rules ! max { ( $x : expr ) => ( $x ) ; ( $x : expr , $ ( $xs : expr ) ,+ ) => { {  std :: cmp :: max ( $x , max! ( $ ( $xs ) ,+ ) ) } } ; }
#[rustfmt::skip]
macro_rules ! min { ( $x : expr ) => ( $x ) ; ( $x : expr , $ ( $xs : expr ) ,+ ) => { {  std :: cmp :: min ( $x , min! ( $ ( $xs ) ,+ ) ) } } ; }
#[rustfmt::skip]
macro_rules! chmin {    ($a:expr, $($b:expr),+) => {{        let d = min!($($b),+);        if $a > d {            $a = d;            true        } else {            false        }    }}; }

#[rustfmt::skip]
macro_rules! chmax {    ($a:expr, $($b:expr),+) => {{        let d = max!($($b),+);        if $a < d {            $a = d;            true        } else {            false        }    }};}

#[rustfmt::skip]
macro_rules! chadd {    ($a:expr, $b:expr) => {{        $a = $a + $b;    }};}
#[rustfmt::skip]
macro_rules! chsub {    ($a:expr, $b:expr) => {{        $a = $a - $b;    }};}
#[rustfmt::skip]
macro_rules! chmul {    ($a:expr, $b:expr) => {{        $a = $a * $b;    }};}

#[cfg(debug_assertions)]
macro_rules! mydbg {
    //($arg:expr) => (dbg!($arg))
    //($arg:expr) => (println!("{:?}",$arg));
      ($($a:expr),*) => {
          eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
      }
}
#[cfg(not(debug_assertions))]
macro_rules! mydbg {
    ($($arg:expr),*) => {};
}

macro_rules! echo {
    ($($a:expr),*) => {
          $(println!("{}",$a))*
      }
}

macro_rules! multivec {
    ($a:expr;$b:expr) => {
        vec![$a; $b]
    };
    ($a:expr;$b:expr;$($c:expr);+) => {
        multivec![vec![$a;$b];$($c);+]
    };
}

use std::cmp::*;
use std::collections::*;
use std::ops::{Add, Div, Mul, Rem, Sub};

fn echoNoReturn() {
    echo!("No");
    std::process::exit(0);
}
fn echoYesReturn() {
    echo!("Yes");
    std::process::exit(0);
}

fn echoNo() {
    echo!("No");
}
fn echoYes() {
    echo!("Yes");
}

#[allow(dead_code)]
static INF_I64: i64 = 92233720368547758;
#[allow(dead_code)]
static INF_I32: i32 = 21474836;
#[allow(dead_code)]
static INF_USIZE: usize = 18446744073709551;
#[allow(dead_code)]
static M_O_D: usize = 1000000007;
#[allow(dead_code)]
static PAI: f64 = 3.1415926535897932;

trait IteratorExt: Iterator {
    fn toVec(self) -> Vec<Self::Item>;
}

impl<T: Iterator> IteratorExt for T {
    fn toVec(self) -> Vec<Self::Item> {
        self.collect()
    }
}

fn sin(a: f64) -> f64 {
    a.sin()
}
fn cos(a: f64) -> f64 {
    a.cos()
}
trait ToNum {
    fn toNum(&self) -> Vec<usize>;
}
impl ToNum for Vec<char> {
    fn toNum(&self) -> Vec<usize> {
        if self[0].is_ascii_digit() {
            self.iter().map(|x| x.toNumIndex()).toVec()
        } else {
            self.iter().map(|x| x.toAlphabetIndex()).toVec()
        }
    }
}

trait CharExt {
    fn toNum(&self) -> usize;
    fn toAlphabetIndex(&self) -> usize;
    fn toNumIndex(&self) -> usize;
}
impl CharExt for char {
    fn toNum(&self) -> usize {
        return *self as usize;
    }
    fn toAlphabetIndex(&self) -> usize {
        return self.toNum() - 'a' as usize;
    }
    fn toNumIndex(&self) -> usize {
        return self.toNum() - '0' as usize;
    }
}
trait PrimeNumber:
    Copy
    + Ord
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + Rem<Output = Self>
{
    fn is_odd(self) -> bool;
    fn is_even(self) -> bool;
    fn pow(self, exp: u32) -> Self;
}

macro_rules! impl_prim_int(($($ty:ty),*) => {
    $(
        impl PrimeNumber for $ty {
            fn pow(self, exp: u32) -> Self {
                self.pow(exp)
            }
            fn is_odd(self)->bool{
                self & 1==1
            }

            fn is_even(self)->bool{
                self & 1==0
            }
        }
    )*
});

trait SafeRangeContain {
    fn safe_contains(&self, x: i64) -> bool;
}

impl SafeRangeContain for std::ops::Range<usize> {
    fn safe_contains(&self, x: i64) -> bool {
        if x < 0 {
            return false;
        }
        return self.contains(&(x as usize));
    }
}

impl_prim_int!(
    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize
);
trait VectorExt {
    fn joinToString(&self, s: &str) -> String;
}
impl<T: ToString> VectorExt for Vec<T> {
    fn joinToString(&self, s: &str) -> String {
        return self
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(s);
    }
}

trait StringExt {
    fn get_reverse(&self) -> String;
}
impl StringExt for String {
    fn get_reverse(&self) -> String {
        self.chars().rev().collect::<String>()
    }
}

trait UsizeExt {
    fn pow(&self, n: usize) -> usize;
}
impl UsizeExt for usize {
    fn pow(&self, n: usize) -> usize {
        return ((*self as u64).pow(n as u32)) as usize;
    }
}

use itertools_num::ItertoolsNum as _;
use superslice::*;

fn main() {
    input! {
       T: usize,
    }

    for _ in 0..T {
        solve();
    }
}

struct Direction {
    H: usize,
    W: usize,
    mx: Vec<i64>,
    my: Vec<i64>,
    direction: Vec<char>,
}
impl Direction {
    fn new(h: usize, w: usize) -> Self {
        Direction {
            H: h,
            W: w,
            mx: vec![0, -1, 0, 1],
            my: vec![1, 0, -1, 0],
            direction: vec!['D', 'L', 'U', 'R'],
        }
    }
    fn getIndex(&self, y: usize, x: usize) -> usize {
        y * self.W + x
    }
    fn getPos(&self, index: usize) -> (usize, usize) {
        (index / self.W, index % self.W)
    }
    fn dir(&self, y: usize, x: usize, k: usize) -> Option<(usize, usize)> {
        let nx = self.mx[k] + x as i64;
        let ny = self.my[k] + y as i64;
        if nx < 0 || ny < 0 || nx >= self.W as i64 || ny >= self.H as i64 {
            None
        } else {
            Some((ny as usize, nx as usize))
        }
    }
}

fn solve() {
    input! {
        H:usize,
        W:usize,
        S:[Chars;H],
    }

    let dir = Direction::new(H, W);
    let S = S
        .iter()
        .map(|x| x.iter().map(|&y| y as usize - 'A' as usize).toVec())
        .toVec();

    let mut dp = vec![vec![vec![vec![usize::MAX / 10; 4]; 3]; W]; H];
    let mut kouho = BinaryHeap::new();
    for i in 0..3 {
        let cost = if i == S[0][0] { 0 } else { 1 };
        dp[0][0][i][3] = cost;
        kouho.push((Reverse(cost), 0, 0, i, 3));
    }
    // direction: vec!['D', 'L', 'U', 'R'],
    let to = vec![0, 1, 2, 3];
    while !kouho.is_empty() {
        let (Reverse(c), i, j, t, d) = kouho.pop().unwrap();
        let mut nd = 0;
        for next in 0..3 {
            match t {
                0 => {
                    nd = d;
                }
                1 => match d {
                    0 => {
                        nd = 3;
                    }
                    1 => {
                        nd = 2;
                    }
                    2 => {
                        nd = 1;
                    }
                    _ => {
                        nd = 0;
                    }
                },
                _ => match d {
                    0 => {
                        nd = 1;
                    }
                    1 => {
                        nd = 0;
                    }
                    2 => {
                        nd = 3;
                    }
                    _ => {
                        nd = 2;
                    }
                },
            }
            if let Some((ny, nx)) = dir.dir(i, j, to[nd]) {
                let cost = if next == S[ny][nx] { 0 } else { 1 };
                if chmin!(dp[ny][nx][next][nd], dp[i][j][t][d] + cost) {
                    kouho.push((Reverse(dp[i][j][t][d] + cost), ny, nx, next, nd));
                }
            }
        }
    }

    let mut ans = usize::MAX;
    for t in 0..3 {
        for d in 0..4 {
            if d == 3 && t == 0 {
                chmin!(ans, dp[H - 1][W - 1][t][d]);
            } else if d == 0 && t == 1 {
                chmin!(ans, dp[H - 1][W - 1][t][d]);
            }
        }
    }
    echo!(ans);
}

Submission Info

Submission Time
Task E - Reflection on Grid
User bqn
Language Rust (rustc 1.89.0)
Score 500
Code Size 8381 Byte
Status AC
Exec Time 937 ms
Memory 94304 KiB

Compile Error

warning: value assigned to `nd` is never read
   --> src/main.rs:286:17
    |
286 |         let mut nd = 0;
    |                 ^^
    |
    = help: maybe it is overwritten before being read?
    = note: `#[warn(unused_assignments)]` on by default

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 36
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 1956 KiB
01_test_00.txt AC 97 ms 2064 KiB
01_test_01.txt AC 203 ms 2152 KiB
01_test_02.txt AC 251 ms 2100 KiB
01_test_03.txt AC 269 ms 2068 KiB
01_test_04.txt AC 304 ms 2272 KiB
01_test_05.txt AC 354 ms 3264 KiB
01_test_06.txt AC 391 ms 6360 KiB
01_test_07.txt AC 436 ms 6668 KiB
01_test_08.txt AC 52 ms 52900 KiB
01_test_09.txt AC 83 ms 78496 KiB
01_test_10.txt AC 216 ms 52872 KiB
01_test_11.txt AC 219 ms 65296 KiB
01_test_12.txt AC 260 ms 52876 KiB
01_test_13.txt AC 266 ms 59692 KiB
01_test_14.txt AC 361 ms 52984 KiB
01_test_15.txt AC 360 ms 55588 KiB
01_test_16.txt AC 621 ms 57356 KiB
01_test_17.txt AC 611 ms 58976 KiB
01_test_18.txt AC 897 ms 92032 KiB
01_test_19.txt AC 858 ms 79584 KiB
01_test_20.txt AC 117 ms 68628 KiB
01_test_21.txt AC 50 ms 53004 KiB
01_test_22.txt AC 139 ms 94304 KiB
01_test_23.txt AC 78 ms 78440 KiB
01_test_24.txt AC 89 ms 52752 KiB
01_test_25.txt AC 107 ms 65196 KiB
01_test_26.txt AC 242 ms 52816 KiB
01_test_27.txt AC 245 ms 55428 KiB
01_test_28.txt AC 937 ms 83756 KiB
01_test_29.txt AC 310 ms 53136 KiB
01_test_30.txt AC 367 ms 53184 KiB
01_test_31.txt AC 332 ms 70500 KiB
01_test_32.txt AC 329 ms 70396 KiB
01_test_33.txt AC 909 ms 83872 KiB
01_test_34.txt AC 909 ms 83884 KiB