結果

問題 No.2064 Smallest Sequence on Grid
ユーザー koba-e964koba-e964
提出日時 2022-09-02 23:28:07
言語 Rust
(1.77.0)
結果
AC  
実行時間 686 ms / 3,000 ms
コード長 2,166 bytes
コンパイル時間 14,493 ms
コンパイル使用メモリ 404,756 KB
実行使用メモリ 20,828 KB
最終ジャッジ日時 2024-04-27 23:09:39
合計ジャッジ時間 21,426 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 0 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 0 ms
6,940 KB
testcase_07 AC 1 ms
6,948 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 38 ms
20,120 KB
testcase_18 AC 36 ms
19,852 KB
testcase_19 AC 38 ms
20,252 KB
testcase_20 AC 442 ms
20,216 KB
testcase_21 AC 517 ms
20,804 KB
testcase_22 AC 338 ms
20,364 KB
testcase_23 AC 365 ms
20,828 KB
testcase_24 AC 363 ms
20,772 KB
testcase_25 AC 360 ms
20,080 KB
testcase_26 AC 686 ms
19,724 KB
testcase_27 AC 666 ms
19,904 KB
testcase_28 AC 37 ms
20,824 KB
testcase_29 AC 548 ms
17,340 KB
testcase_30 AC 32 ms
19,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// 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 ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    input! {
        h: usize, w: usize,
        s: [String; h],
    }
    let mut s = s;
    let mut tmp = vec![];
    for i in 0..h {
        tmp.extend(s[i].bytes());
        let _ = std::mem::replace(&mut s[i], "".to_string());
    }
    let mut que = vec![(0, 0)];
    let mut ans = "".to_string();
    ans.push(tmp[0] as char);
    for _ in 0..h + w - 2 {
        let mut new = vec![];
        let mut mi = b'{';
        for &(x, y) in &que {
            if x + 1 < h {
                mi = min(mi, tmp[(x + 1) * w + y]);
            }
            if y + 1 < w {
                mi = min(mi, tmp[x * w + y + 1]);
            }
        }
        for &(x, y) in &que {
            if x + 1 < h {
                if mi == tmp[(x + 1) * w + y] {
                    new.push((x + 1, y));
                }
            }
            if y + 1 < w {
                if mi == tmp[x * w + y + 1] {
                    new.push((x, y + 1));
                }
            }
        }
        ans.push(mi as char);
        new.sort(); new.dedup();
        que = new;
    }
    println!("{}", ans);
}
0