提出 #16152316


ソースコード 拡げる

#![allow(
  non_snake_case,
  unused_variables,
  unused_assignments,
  unused_mut,
  unused_imports,
  dead_code,
  unused_macros
)]
//use proconio::fastout;
use proconio::input;
use proconio::marker::*;
//use std::collections::HashSet;
use std::cmp::*;
use std::collections::*;
macro_rules! debug {
  ($($a:expr),* $(,)*) => {
      #[cfg(debug_assertions)]
      eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*);
  };
}
fn main() {
  input! {
     H:usize,
     W:usize,
     C:(Usize1, Usize1),
     D:(Usize1, Usize1),
     S:[Chars;H]
  }
  let C: (usize, usize) = (C.0 + 2, C.1 + 2);
  let D: (usize, usize) = (D.0 + 2, D.1 + 2);
  let S: Vec<Vec<char>> = S;
  let mut kabe = vec![vec![true; W + 10]; H + 10];
  for x in 0..H {
    for y in 0..W {
      if S[x][y] == '.' {
        kabe[x + 2][y + 2] = false;
      }
    }
  }
  let kabe: Vec<Vec<bool>> = kabe;
  // Cを基準に幅優先探索。
  // (n=ワープ回数, 位置)
  let mut h = BinaryHeap::new();
  //訪問済み
  let mut v = vec![vec![false; W + 10]; H + 10];
  // ワープ回数
  let mut d = vec![vec![std::u64::MAX; W + 10]; H + 10];
  v[C.0][C.1] = true;
  d[C.0][C.1] = 0;
  h.push((Reverse(0), C.0, C.1));
  while let Some((Reverse(c), x, y)) = h.pop() {
    toho(&kabe, &mut d, &mut h, c, x - 1, y);
    toho(&kabe, &mut d, &mut h, c, x + 1, y);
    toho(&kabe, &mut d, &mut h, c, x, y - 1);
    toho(&kabe, &mut d, &mut h, c, x, y + 1);

    warp(&kabe, &mut d, &mut h, c, x - 2, y - 2);
    warp(&kabe, &mut d, &mut h, c, x - 2, y - 1);
    warp(&kabe, &mut d, &mut h, c, x - 2, y);
    warp(&kabe, &mut d, &mut h, c, x - 2, y + 1);
    warp(&kabe, &mut d, &mut h, c, x - 2, y + 2);
    warp(&kabe, &mut d, &mut h, c, x - 1, y - 2);
    warp(&kabe, &mut d, &mut h, c, x - 1, y - 1);
    warp(&kabe, &mut d, &mut h, c, x - 1, y + 1);
    warp(&kabe, &mut d, &mut h, c, x - 1, y + 2);
    warp(&kabe, &mut d, &mut h, c, x, y - 2);
    warp(&kabe, &mut d, &mut h, c, x, y + 2);
    warp(&kabe, &mut d, &mut h, c, x + 1, y - 2);
    warp(&kabe, &mut d, &mut h, c, x + 1, y - 1);
    warp(&kabe, &mut d, &mut h, c, x + 1, y + 1);
    warp(&kabe, &mut d, &mut h, c, x + 1, y + 2);
    warp(&kabe, &mut d, &mut h, c, x + 2, y - 2);
    warp(&kabe, &mut d, &mut h, c, x + 2, y - 1);
    warp(&kabe, &mut d, &mut h, c, x + 2, y);
    warp(&kabe, &mut d, &mut h, c, x + 2, y + 1);
    warp(&kabe, &mut d, &mut h, c, x + 2, y + 2);
  }
  let ans = d[D.0][D.1];
  if ans < std::u64::MAX {
    println!("{}", ans);
  } else {
    println!("-1");
  }
}

fn warp(
  kabe: &Vec<Vec<bool>>,
  d: &mut Vec<Vec<u64>>,
  h: &mut BinaryHeap<(Reverse<u64>, usize, usize)>,
  c: u64,
  x: usize,
  y: usize,
) -> () {
  if kabe[x][y] == false {
    let c = c + 1;
    if d[x][y] > c {
      d[x][y] = c;
      h.push((Reverse(c), x, y));
    }
  }
}

fn toho(
  kabe: &Vec<Vec<bool>>,
  d: &mut Vec<Vec<u64>>,
  h: &mut BinaryHeap<(Reverse<u64>, usize, usize)>,
  c: u64,
  x: usize,
  y: usize,
) -> bool {
  if kabe[x][y] == false {
    if d[x][y] > c {
      d[x][y] = c;
      h.push((Reverse(c), x, y));
    }
    true
  } else {
    false
  }
}

提出情報

提出日時
問題 D - Wizard in Maze
ユーザ aoriso
言語 Rust (1.42.0)
得点 400
コード長 3139 Byte
結果 AC
実行時間 323 ms
メモリ 17340 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 400 / 400
結果
AC × 3
AC × 14
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, max_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_11.txt, random_12.txt, sample_01.txt, sample_02.txt, sample_03.txt
ケース名 結果 実行時間 メモリ
hand_01.txt AC 9 ms 2036 KiB
hand_02.txt AC 2 ms 1984 KiB
max_01.txt AC 43 ms 17052 KiB
random_01.txt AC 25 ms 16812 KiB
random_02.txt AC 22 ms 3720 KiB
random_03.txt AC 90 ms 17136 KiB
random_04.txt AC 48 ms 3684 KiB
random_05.txt AC 323 ms 17340 KiB
random_06.txt AC 105 ms 6932 KiB
random_11.txt AC 10 ms 2316 KiB
random_12.txt AC 3 ms 2180 KiB
sample_01.txt AC 2 ms 2008 KiB
sample_02.txt AC 2 ms 2072 KiB
sample_03.txt AC 2 ms 2004 KiB