結果

問題 No.340 雪の足跡
ユーザー koba-e964koba-e964
提出日時 2021-11-18 00:07:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 513 ms / 1,000 ms
コード長 2,874 bytes
コンパイル時間 3,192 ms
コンパイル使用メモリ 158,260 KB
実行使用メモリ 84,172 KB
最終ジャッジ日時 2023-08-25 21:17:33
合計ジャッジ時間 10,933 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 19 ms
8,008 KB
testcase_15 AC 22 ms
8,304 KB
testcase_16 AC 15 ms
4,972 KB
testcase_17 AC 28 ms
9,360 KB
testcase_18 AC 25 ms
9,328 KB
testcase_19 AC 27 ms
9,324 KB
testcase_20 AC 213 ms
46,184 KB
testcase_21 AC 103 ms
9,984 KB
testcase_22 AC 24 ms
37,060 KB
testcase_23 AC 277 ms
83,924 KB
testcase_24 AC 173 ms
41,236 KB
testcase_25 AC 253 ms
57,560 KB
testcase_26 AC 29 ms
4,380 KB
testcase_27 AC 6 ms
4,376 KB
testcase_28 AC 29 ms
5,652 KB
testcase_29 AC 348 ms
49,720 KB
testcase_30 AC 196 ms
38,324 KB
testcase_31 AC 451 ms
77,100 KB
testcase_32 AC 511 ms
83,960 KB
testcase_33 AC 470 ms
84,172 KB
testcase_34 AC 513 ms
83,908 KB
testcase_35 AC 490 ms
83,932 KB
testcase_36 AC 488 ms
83,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    input! {
        w: usize, h: usize, n: usize,
        b: [([usize], usize); n],
    }
    let mut g = vec![vec![]; h * w];
    let mut row = vec![vec![0; w]; h];
    let mut col = vec![vec![0; h]; w];
    for (mut b, blast) in b {
        b.push(blast);
        let m = b.len();
        for i in 0..m - 1 {
            let (x, y) = if b[i] < b[i + 1] {
                (b[i], b[i + 1])
            } else {
                (b[i + 1], b[i])
            };
            if x % w == y % w {
                col[x % w][x / w] += 1;
                col[x % w][y / w] -= 1;
            } else {
                row[x / w][x % w] += 1;
                row[x / w][y % w] -= 1;
            }
        }
    }
    for i in 0..h {
        for j in 0..w - 1 {
            row[i][j + 1] += row[i][j];
            if row[i][j] > 0 {
                g[i * w + j].push(i * w + j + 1);
                g[i * w + j + 1].push(i * w + j);
            }
        }
    }
    for i in 0..w {
        for j in 0..h - 1 {
            col[i][j + 1] += col[i][j];
            if col[i][j] > 0 {
                g[j * w + i].push(j * w + i + w);
                g[j * w + i + w].push(j * w + i);
            }
        }
    }
    let mut que = VecDeque::new();
    const INF: i32 = 1 << 28;
    let mut dist = vec![INF; h * w];
    que.push_back((0, 0));
    while let Some((d, v)) = que.pop_front() {
        if dist[v] <= d { continue; }
        dist[v] = d;
        for &w in &g[v] {
            if dist[w] > d + 1 {
                que.push_back((d + 1, w));
            }
        }
    }
    if dist[h * w - 1] >= INF {
        println!("Odekakedekinai..");
    } else {
        println!("{}", dist[h * w - 1]);
    }
}
0